On Wednesday, October 30, 2013 12:10:49 PM UTC-6, Michael Bayer wrote: > > > On Oct 30, 2013, at 1:43 PM, jkmacc <[email protected] <javascript:>> > wrote: > > > Hmm... The 'schema' problem was my dumb mistake, but I still haven't > found a use of @declared_attr that solves my problem. It looks like, by > the time I get to my OldStudents or anything afterwards, I have to > redeclare almost everything in __table_args__. I've tried modifying the > base with a @declared_attr that merges __table_args__ down the class > hierarchy, like in > https://groups.google.com/forum/#!topic/sqlalchemy/KybuUktY3t8, but I > just get recursion problems. I'd hoped that I could declare most > everything once in the abstract class, but it seems that what you're saying > is that this isn't true. > > > I’m not seeing that as the case, I can apply the @declared_attr just to > the top __table_args__ and that’s all that’s needed, everything works out > fine: > > from sqlalchemy import Column, Numeric, String, Date, PrimaryKeyConstraint > from sqlalchemy.ext.declarative import declarative_base, declared_attr > > Base = declarative_base() > > class Students(Base): > __abstract__ = True > > @declared_attr > def __table_args__(cls): > return (PrimaryKeyConstraint(u'stid', u'last_name'),) > > stid = Column(Numeric(9, 0, False), nullable=False, info={'format': > '9.2f'}) > first_name = Column(String(30), info={'format': '30.30s'}) > last_name = Column(String(30), info={'format': '30.30s'}) > description = Column(String(80), info={'format': '80.80s'}) > lddate = Column(Date, info={'format': '%Y-%m-%d %H:%M:%S'}) > > class MyStudents(Students): > __tablename__ = 'students' > __table_args__ = Students.__table_args__ + ({'schema': 'me'},) > > class OldStudents(Students): > __tablename__ = 'oldstudents' > __table_args__ = Students.__table_args__ + ({'schema': 'me'},) > > class OtherStudents(Students): > __tablename__ = 'students' > __table_args__ = Students.__table_args__ + ({'schema': 'other'},) > > # schema works > assert OtherStudents.__table__.schema == “other" > > # primary key works > assert list(OtherStudents.__table__.primary_key) == \ > [OtherStudents.__table__.c.stid, > OtherStudents.__table__.c.last_name] > > # what’s not working? > > > Oh, _that's_ how you use @declared_attr. Fantastic. This works wonderfully!
Now, to implement some that merge all __table_args__ in the hierarchy, like http://www.sqlalchemy.org/trac/ticket/2700, but will also take a dictionary. If so, perhaps one could avoid the "Students.__table_args__ +" step and just say "__table_args__" = {'schema': 'other'}" for the children. Thanks for your help! -Jon -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
