On 10/14/2016 11:44 AM, Seth P wrote:
I have a mixin of the following form

class MyMixin(object):
    idx = sa.Column(sa.Integer, sa.Sequence('idx_seq', schema=???, 
optional=True), primary_key=True)
    ...


I would like the sequence to have the same schema as the table into
which MyMixin will be mixed. I realize I could make idx a declared_attr,
and then extract the schema from the provided cls -- and this is
probably the "right" solution -- but that would lead to the idx column
going at the end of the column list, and I really want it at the
beginning. (Yes, this is just for silly aesthetics, but *all* my tables
start with idx, and I'd like ones using MyMix to do so too.)

Is it possible to define the idx column without the sequence, and then
afterwards create the sequence and add it to the column? Or
alternatively define the sequence's schema at some later stage? Or
better yet, is there some magic setting of the sequence's schema that
will have it "inherit" from the containing table (I realize that in
general sequences don't have "containing tables")?

The simplest way is probably to set the creation order of the column to be at the top:

col = Column(...)
col._creation_order = -100


Being able to set that ordering as part of public API is probably something that should be added as public API; I'd call it something like "sort_key".


Other way, you can set the Sequence after the fact, either by:

my_column.default = Sequence(...)

The above is not the "legit" way because it doesn't fire off the parent attachment events, which at the moment aren't necessary in this case.

or the more "legit" way, and again public API should someday support this more fully (including that if one assigns to column.default, column.server_default, it would "work" / emit a warning or something), like this:

seq = Sequence()
seq._set_parent(my_column)


If you are doing the "create the sequence later" version you can use various mapper or class instrumentation event hooks (see the docs) or the __declare_first__() hook to stick the sequence on there.

Since none of these methods are fully public API yet, _creation_order is likely the simplest to use and also the easiest one for us to make public at some point.










--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and
Verifiable Example. See http://stackoverflow.com/help/mcve for a full
description.
---
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to