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"
classLevel(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()
classLevel(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'
otherwise it makes no sense, if two threads are running and in one
thread they set Level.set_tablename('foo') and another sets
Level.set_tablename('bar'), they'd conflict.
Here's another discussion that shows a similar example:
https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/EntityName
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+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
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.