On 08/12/2011 02:18 PM, werner wrote:
I can't figure out how I could adapt the pk_col function on this page
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/NamingConventions to
handle the Sequence definition needed for e.g. Firebird.
At the point when Column is instantiated I don't have access to
"table.name" and I can't figure it out either how to do it in
on_table_attach.
Would appreciate any tips on this.
Did a bit more searching and trying and came up with this:
def pk_col(cls, **kw):
"""Produce a primary key column for a table.
e.g.::
pk_col()
is equivalent to::
Column("id", sa.BigInteger,
doc = "Primary key column for <tablename>",
primary_key=True,
sequence=sa.Sequence('<tablename>_id')
)
"""
kw['primary_key'] = True
c = sa.Column(sa.BigInteger(), sa.Sequence('seq_%s_%s' %
(cls.__tablename__,
dbg.pkId)), **kw)
@sa.event.listens_for(c, "before_parent_attach")
def on_table_attach(column, table):
column.name = column.key = dbg.pkId
column.doc = "Primary key column for %r" % table.name
c._creation_order = 0 # forces it to the top when using declarative
return c
I.e. pass "cls" in so I can get to __tablename__.
Is this an o.k. way of doing it or is there a better/cleaner way?
Werner
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.