Re: [sqlalchemy] Re: Column order with declarative base

2014-12-30 Thread Sven Teresniak

Am Freitag, 2. Juli 2010 02:24:05 UTC+2 schrieb Michael Bayer:

 The Column object contains a sort key when constructed, against a single 
 global value, that is used as a sort key when the Table is generated.  This 
 is to get around the fact that the attribute dictionary of the declarative 
 class is unordered.   

 The mixin columns should copy their sort key over, or it should somehow 
 be tailored in the declarative base so that the order of the two columns 
 stays relatively the same, and perhaps is also tailored to be at the same 
 position relative to the other columns in the ultimate table.

 I'd welcome any patches in this regard since I don't usually deal with the 
 mixin feature.

Is there any simple way to modify/set this sort key or is there any way 
for me to workaround this random ordering in the class dict? Or to simple 
inspect the ordering to generate code that re-orders my primary composite 
key parts accordingly?

Thanks
Sven

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Re: Column order with declarative base

2014-12-30 Thread Michael Bayer
why don’t you set up your PrimaryKeyConstraint directly?

class AbstractPK(NameByClass):
 this table defines a frequently used composite primary key 

@declared_attr
def key1(cls):
return Column(ForeignKey(somekey.keypart1), primary_key=True)

@declared_attr
def key2(cls):
return Column(ForeignKey(anotherkey.keypart2), primary_key=True)

key3 = Column( Integer, primary_key=True )

@declared_attr
def __table_args__(self):
return (
PrimaryKeyConstraint('key1', 'key2', 'key3'),
)




Sven Teresniak realkno...@gmail.com wrote:

 
 Am Freitag, 2. Juli 2010 02:24:05 UTC+2 schrieb Michael Bayer:
 The Column object contains a sort key when constructed, against a single 
 global value, that is used as a sort key when the Table is generated.  This 
 is to get around the fact that the attribute dictionary of the declarative 
 class is unordered.  
 
 The mixin columns should copy their sort key over, or it should somehow be 
 tailored in the declarative base so that the order of the two columns stays 
 relatively the same, and perhaps is also tailored to be at the same position 
 relative to the other columns in the ultimate table.
 
 I'd welcome any patches in this regard since I don't usually deal with the 
 mixin feature.
 
 Is there any simple way to modify/set this sort key or is there any way for 
 me to workaround this random ordering in the class dict? Or to simple inspect 
 the ordering to generate code that re-orders my primary composite key parts 
 accordingly?
 
 Thanks
 Sven
 
 -- 
 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 http://groups.google.com/group/sqlalchemy 
 http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout 
 https://groups.google.com/d/optout.

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Re: Column order with declarative base

2014-12-30 Thread Sven Teresniak
Aaah Michael,
thanks!
This is awesome!

I tried a lot and all the time I felt that I missed exact this kind of easy 
answer. ;)

Thanks again.
Will implement this now.

Sven

Am Dienstag, 30. Dezember 2014 17:01:14 UTC+1 schrieb Michael Bayer:

 why don’t you set up your PrimaryKeyConstraint directly?

 class AbstractPK(NameByClass):
  this table defines a frequently used composite primary key 

 @declared_attr
 def key1(cls):
 return Column(ForeignKey(somekey.keypart1), primary_key=True)

 @declared_attr
 def key2(cls):
 return Column(ForeignKey(anotherkey.keypart2), primary_key=True)

 key3 = Column( Integer, primary_key=True )

 @declared_attr
 def __table_args__(self):
 return (
 PrimaryKeyConstraint('key1', 'key2', 'key3'),
 )




 Sven Teresniak realk...@gmail.com javascript: wrote:


 Am Freitag, 2. Juli 2010 02:24:05 UTC+2 schrieb Michael Bayer:

 The Column object contains a sort key when constructed, against a 
 single global value, that is used as a sort key when the Table is 
 generated.  This is to get around the fact that the attribute dictionary of 
 the declarative class is unordered.   

 The mixin columns should copy their sort key over, or it should somehow 
 be tailored in the declarative base so that the order of the two columns 
 stays relatively the same, and perhaps is also tailored to be at the same 
 position relative to the other columns in the ultimate table.

 I'd welcome any patches in this regard since I don't usually deal with 
 the mixin feature.

 Is there any simple way to modify/set this sort key or is there any way 
 for me to workaround this random ordering in the class dict? Or to simple 
 inspect the ordering to generate code that re-orders my primary composite 
 key parts accordingly?

 Thanks
 Sven

 -- 
 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+...@googlegroups.com javascript:.
 To post to this group, send email to sqlal...@googlegroups.com 
 javascript:.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.



-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Re: Column order with declarative base and @declared_attr

2011-07-21 Thread Hans-Martin
 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.

Yip, did that first -- but then these appeared always before any
columns declared via @declared_attr (no surprise, if I my very limited
understanding of SQLAlchemy's internals is correct). Unfortunately,
I'd like to have the columns with foreign keys up front :-(

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

Would be wonderful... Although it's more of a nuisance than a real
problem, it only really affects browsing through the tables after
all.

Thanks,
Hans-Martin

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



Re: [sqlalchemy] Re: Column order with declarative base

2010-07-02 Thread Chris Withers

Michael Bayer wrote:

I'd welcome any patches in this regard since I don't usually deal with the 
mixin feature.


fine, how about Chris works up the unit test for it:


Done and pushed back to tip with the fix.

cheers,

Chris

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



[sqlalchemy] Re: Column order with declarative base

2010-07-02 Thread Mike Lewis
On Jul 1, 2010, at 8:24 PM, Michael Bayer wrote:

 The Column object contains a sort key when constructed, against a single 
 global value, that is used as a sort key when the Table is generated.  This 
 is to get around the fact that the attribute dictionary of the declarative 
 class is unordered.  


Interesting.  I guess I didn't come across this in the source.  I
actually ended up implementing a similar thing in a subclass I have of
column (I think I'm doing some stuff that isn't very standard)

Since we're talking about columns, I also noticed that copy() doesn't
copy the .info object over.

 The mixin columns should copy their sort key over, or it should somehow be 
 tailored in the declarative base so that the order of the two columns stays 
 relatively the same, and perhaps is also tailored to be at the same position 
 relative to the other columns in the ultimate table.

 I'd welcome any patches in this regard since I don't usually deal with the 
 mixin feature.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



[sqlalchemy] Re: Column order with declarative base

2010-07-01 Thread Mike Lewis

 Please provide a simple, small example of your problem :-)

 Also, is there a reason the order of column creation matters?

 Chris

 --
 Simplistix - Content Management, Batch Processing  Python Consulting
             -http://www.simplistix.co.uk

class Foo(object):
  id = Column(Integer, primary_key=True)
  foo = Column(Integer, nullable=True)

class Bar(Base, object):
  __tablename__ = 'bar'

then on creation of bar, foo might be first.  I believe this is
because DeclarativeBase adds new documns with dir() and that returns
stuff in an arbitrary order.

I'm trying to use __metaclass__ to make a metaclass for Foo where it
retains order of the dictionary, but I am getting stumped.

Thanks,
Mike

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] Re: Column order with declarative base

2010-07-01 Thread Michael Bayer

On Jul 1, 2010, at 6:35 PM, Mike Lewis wrote:

 
 Please provide a simple, small example of your problem :-)
 
 Also, is there a reason the order of column creation matters?
 
 Chris
 
 --
 Simplistix - Content Management, Batch Processing  Python Consulting
 -http://www.simplistix.co.uk
 
 class Foo(object):
  id = Column(Integer, primary_key=True)
  foo = Column(Integer, nullable=True)
 
 class Bar(Base, object):
  __tablename__ = 'bar'
 
 then on creation of bar, foo might be first.  I believe this is
 because DeclarativeBase adds new documns with dir() and that returns
 stuff in an arbitrary order.
 
 I'm trying to use __metaclass__ to make a metaclass for Foo where it
 retains order of the dictionary, but I am getting stumped.

The Column object contains a sort key when constructed, against a single 
global value, that is used as a sort key when the Table is generated.  This is 
to get around the fact that the attribute dictionary of the declarative class 
is unordered.   

The mixin columns should copy their sort key over, or it should somehow be 
tailored in the declarative base so that the order of the two columns stays 
relatively the same, and perhaps is also tailored to be at the same position 
relative to the other columns in the ultimate table.

I'd welcome any patches in this regard since I don't usually deal with the 
mixin feature.


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] Re: Column order with declarative base

2010-07-01 Thread Michael Bayer

On Jul 1, 2010, at 8:24 PM, Michael Bayer wrote:

 
 The Column object contains a sort key when constructed, against a single 
 global value, that is used as a sort key when the Table is generated.  This 
 is to get around the fact that the attribute dictionary of the declarative 
 class is unordered.   
 
 The mixin columns should copy their sort key over, or it should somehow be 
 tailored in the declarative base so that the order of the two columns stays 
 relatively the same, and perhaps is also tailored to be at the same position 
 relative to the other columns in the ultimate table.
 
 I'd welcome any patches in this regard since I don't usually deal with the 
 mixin feature.

fine, how about Chris works up the unit test for it:

diff -r af4bdd33564e lib/sqlalchemy/ext/declarative.py
--- a/lib/sqlalchemy/ext/declarative.py Thu Jul 01 16:57:02 2010 -0400
+++ b/lib/sqlalchemy/ext/declarative.py Thu Jul 01 20:25:59 2010 -0400
@@ -673,7 +673,8 @@
 if name not in dict_ and not (
 '__table__' in dict_ and name in 
dict_['__table__'].c
 ):
-
potential_columns[name]=column_copies[obj]=obj.copy()
+potential_columns[name] = column_copies[obj] = 
obj.copy()
+column_copies[obj]._creation_order = 
obj._creation_order
 elif isinstance(obj, RelationshipProperty):
 raise exceptions.InvalidRequestError(
 relationships are not allowed on 



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.