On Aug 12, 2011, at 9:04 AM, werner wrote: > 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?
you should be able to set the Sequence name directly in the attach event (starting with a fake name). not sure how you're using pk_col() above (how it gets at 'cls'). Its also possible to create + attach the Sequence to the Column after the fact but I don't know that the public API is there for that quite yet. (i think calling seq._set_parent(column) would be sufficient ...) -- 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.
