On Oct 30, 2013, at 10:58 AM, jkmacc <jkm...@gmail.com> wrote:

> Hi all,
> 
> I've got a module defining a number of abstract declarative classes.  They 
> represent standard generic classes/tables.  
> I'd like to take one of these abstract classes and subclass more than once, 
> adding a 'schema' name to '__table_args__' and a '__tablename__'.  This to me 
> is like making a specific 'realization' of the abstract class structure.  I 
> want to keep the columns (with info dictionaries) and constraints in the 
> abstract class, but make a 'realization' of the abstract class as different 
> tables with different names and schema.  
> 
> When I do this, however, I get warnings and errors I don't understand. Here's 
> an example:
> 
> % ------------------------  code  
> --------------------------------------------------------------------------
> from sqlalchemy import Column, Numeric, String, Date, PrimaryKeyConstraint
> from sqlalchemy.ext.declarative import declarative_base
> 
> Base = declarative_base()
> 
> class Students(Base):
>     __abstract__ = True
>     __table_args__ = (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'},)
> % ------------------------  end code  ———————————————————————————————————


well the “schema” argument on Table is called “schema”, not “__schema__”, and 
the __table_args__ are not copied to each subclass (e.g. you’re using the same 
PrimaryKeyConstraint object on three different Table objects) so you’d want to 
use @declared_attr + def __table_args__(cls) for that.


-- 
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/groups/opt_out.

Reply via email to