On Tuesday, August 18, 2015 at 11:24:08 PM UTC-6, Michael Bayer wrote: > > > > On 8/18/15 8:57 PM, Frank Horowitz wrote: > > Hi All, > > A noob question, that I've been struggling with off and on for a while. > I'm pretty sure that I'm missing something obvious... > > I'm creating tables and their entries "on the fly" programmatically, and > need to control the table names. > > I understand how to define declarative classes as follows: > Base = declarative_base() > levels_name = "foobar" > class Level(Base): > __tablename__ = levels_name > height = Column(Float) > That works well when the static string "foobar" is in the class definition > module. > > What I can't figure out is how to name a table at "construction time" with > a dynamic string. In other words, how do I specify a tablename when I first > _use_ a declarative class? > > A perfect scenario for me would be something like: > Base = declarative_base() > class Level(Base): > height = Column(Float) > > lvl = Level.set_tablename('foobar') > > lvl.height = 100. > > and have that show up in a table 'foobar', but I can't figure out any way > to code set_tablename() ... > > it doesn't work that way. The tables in your schema are supposed to be > fixed. If you want to map a new class to a new table, you can do that with > mixins: > > > class Level(object): > id = Column(Integer, primary_key=True) > height = Column(Float) > > > > class LevelFoobar(Base, Level): > __tablename__ = 'foobar' > > class LevelBat(Base, Level): > __tablename__ = 'bat' > >
> Is this significantly different from using __abstract__? e.g. > class Level(Base): __abstract__ = True id = Column(Integer, primary_key=True) height = Column(Float) class LevelFoobar(Level): __tablename__ = 'foobar' class LevelBat(Level): __tablename__ = 'bat' > > > > > In case it matters, my backend is Postgres 9.4.0.1 ... > > What am I missing? > > TIA for any advice! > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+...@googlegroups.com <javascript:>. > To post to this group, send email to sqlal...@googlegroups.com > <javascript:>. > Visit this group at http://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. > > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.