On 08/12/2011 04:46 PM, Michael Bayer wrote:
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').
I had this as part of the declarative base.
    @sad.declared_attr
    def id(cls):
        # use a method so that the pk_col() returned
        # here is the one used instead of a copy
        return pk_col(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 ...)

Yeap, great.

got this now:
    @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
        seq = sa.Sequence('seq_%s_%s' % (table.name,
                                         dbg.pkId))
        seq._set_parent(column)

I guess I just have to watch out for whenever you provide a public API for _set_parent.

Thanks
Werner

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to