This is the "entity name" recipe, and we have a classical and declarative 
version over at http://www.sqlalchemy.org/trac/wiki/UsageRecipes/EntityName .   
 It's using just the straight type() factory.     

You definitely want to use distinct Column objects for each class, this because 
a Column object is immediately made to reference its parent Table, for obvious 
reasons.   Recipes that generate multiple tables from a single set of Column 
objects use the copy() method on Column for this purpose.   However, you don't 
even need to deal with that, since declarative mixins take care of the copy() 
stuff for you.  I've updated the EntityName example to illustrate taking 
advantage of the mixin so check it out.




On Dec 7, 2010, at 10:45 AM, Tarek Ziadé wrote:

> Hey,
> 
> I have a declarative table called 'Foo':
> 
> _Base = declarative_base()
> 
> class Foo(_Base):
>    __tablename__ = 'foo'
>    __table_args__ = {'mysql_engine': 'InnoDB',
>                              'mysql_charset': 'latin1'}
>    id = Column(String(64), primary_key=True, autoincrement=False)
>    stuff = Column(Integer(11), primary_key=True, nullable=False)
> 
> Now I create Foo1 that has exactly the same definition but differs
> just by the name:
> 
> class Foo1(_Base):
>    __tablename__ = 'foo1'
>    __table_args__ = {'mysql_engine': 'InnoDB',
>                              'mysql_charset': 'latin1'}
>    id = Column(String(64), primary_key=True, autoincrement=False)
>    stuff = Column(Integer(11), primary_key=True, nullable=False)
> 
> 
> And I need more of those (FooN with N = 10). The use case is to shard
> data across several tables.
> 
> To avoid cut/n/pastes, I tried inheritance,  meta-class, a type
> factory and the like, but everything fails. Whether because I was
> reusing the same column class objects or whether because I was
> conflicting with SQLALchemy meta-class magic.
> 
> Is there any sane way to do this and avoid a crazy copy/paste ?
> 
> Cheers
> Tarek
> 
> -- 
> Tarek Ziadé | http://ziade.org
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to