I am creating a bunch of columns by means of the declared_attr decorator since many of them contain foreign keys. Similar to the issue in this thread [1] from a year ago, it seems that the column order is not preserved. To shamelessly borrow the example from that thread, when doing:
class Foo(object):
@declared_attr
def id(self):
return Column(Integer, primary_key=True)
@declared_attr
def foo(self):
return Column(Integer, nullable=True)
class Bar(Base, object):
__tablename__ = 'bar'
then the order of 'foo' and 'id' appears to be random. Is there a way around this?
FWIW, I'm using SQLAlchemy 0.7.1 on Python 3.2, using SQLite as the backend.
Thanks,
Hans-Martin
[1] http://groups.google.com/group/sqlalchemy/browse_thread/thread/0c8aa608c8dd4d7a
> Hi,
>
> I am creating a bunch of columns by means of the declared_attr decorator since many of them contain foreign keys. Similar to the issue in this thread [1] from a year ago, it seems that the column order is not preserved. To shamelessly borrow the example from that thread, when doing:
>
>
> class Foo(object):
>
> @declared_attr
> def id(self):
> return Column(Integer, primary_key=True)
>
> @declared_attr
> def foo(self):
> return Column(Integer, nullable=True)
>
> class Bar(Base, object):
>
> __tablename__ = 'bar'
>
>
> then the order of 'foo' and 'id' appears to be random. Is there a way around this?
>
> FWIW, I'm using SQLAlchemy 0.7.1 on Python 3.2, using SQLite as the backend.
The order is determined by the order in which the actual Column constructor is called, and an ordering token is applied. When a mixin is used, the Column is copied from the mixin, but it's likely that the ordering token is preserved. You'd declare them on Foo directly without using a @declared_attr function. Columns that have no ForeignKey can be created in this way.
There's no other way to approach this as the dictionary created by a new class is unordered in Python (unless perhaps we added ordering tokens to the usage of @declared_attr also, that could work).
the @declarative_attr object would need util.set_creation_order() applied and the _MapperConfig._scan_attributes() would need to take this into account. However, it would not behave well across multiple mixins. The mixins must be scanned in __mro__ order first. So it would be of limited use for there to be an ordering under @declared_attr. On 08/25/2016 02:46 PM, Seth P wrote: > I was just bitten by this issue. Is it still the case that there is no way to specify the order of two columns declared in a mixin using @declared_attr? > -- You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group. To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/OA-n_pY0tuM/unsubscribe. To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com. To post to this group, send email to sqlal...@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.