On Jul 21, 2011, at 4:07 PM, Hans-Martin v. Gaudecker wrote:

> 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).


-- 
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