thank's a lot ! it work's i had the class object to globals() for using the object when i load my module
=============================================================================================== def add_a_table(tablename): ClasseTable = DeclarativeMeta("Part_%s"%tablename, (BaseTest,), { '__tablename__':tablename, 'id':Column('ID', Integer, primary_key=True, nullable=False ), 'libel':Column('LIBEL', String(20), nullable=False), 'description':Column('LABEL', String(50), nullable=False), 'partNumber':Column('PARTNUMBER', String(19), nullable=False) }) # spécifie dans le module l'objet classe table globals()['Part_%s'%country] = ClasseTable return ClasseTable =============================================================================================== On 29 mai, 18:58, "Michael Bayer" <mike...@zzzcomputing.com> wrote: > you need to also specify the desired columns: > > return DeclarativeMeta("%sMyBase"%tablename, (BaseTest,), { > '__tablename__':tablename, > 'id':Column('id', Integer, primary_key=True), > 'stuff':Column('stuff', String(20)) > }) > > sbard wrote: > > > On 28 mai, 17:54, "Michael Bayer" <mike...@zzzcomputing.com> wrote: > >> sbard wrote: > > >> > hello, > >> > i've got a database with one table per country. > >> > for example : > >> > part_es (for spain) > >> > part_uk (....) > >> > part_it > > >> > each tables have got the same schema > >> > (id, libel, description, part_numer) > >> > same __init__ method > >> > same __repr__ method > > >> > is there an easy way to avoid code multiplication and simplify a new > >> > country integration > >> > inheritance way throw me away ... > > >> > if i've got somewhere the list of possible country, is there any way > >> > too loop and add > >> > to declarative_base ? > > >> > dont' hit me for such dummy/newbie question > > >> map anonymous classes. if you're already on declarative the metaclass > >> should do it for you: > > >> from sqlalchemy.ext.declarative import DeclarativeMeta > >> def add_a_table(tablename): > >> return DeclarativeMeta("%sMyBase" % tablename, (MyBase,), > >> {'__tablename__':tablename} > > > i make a sample with youre snippet but it's not working : > > and in fact i don't understand the way you're using DeclarativeMeta > > > #------------------------------------------------------------------------------- > > import sqlalchemy > > from sqlalchemy import create_engine > > from sqlalchemy.orm import sessionmaker > > > #------------------------------------------------------------------------------- > > # THE DECLARATIVE PART > > > from sqlalchemy.orm import mapper > > from sqlalchemy.orm import relation > > from sqlalchemy.orm import backref > > > from sqlalchemy import create_engine > > from sqlalchemy import Table, Column, Integer, Date, Numeric, > > DateTime, Float, Boolean, String, MetaData, ForeignKey, Index, Text > > from sqlalchemy.ext.declarative import declarative_base > > > from sqlalchemy.schema import UniqueConstraint > > from sqlalchemy.schema import Sequence > > from sqlalchemy import and_ > > > BaseTest = declarative_base() > > > class Part(BaseTest): > > """ > > french part Table > > """ > > __tablename__ = 'part_fr' > > > id = Column('ID', Integer, primary_key=True, nullable=False ) > > libel = Column('LIBEL', String(20), nullable=False) > > description = Column('LABEL', String(50), nullable=False) > > part_number = Column('PARTNUMBER', String(19), nullable=False) > > > __table_args__ = {'mysql_engine':'MyISAM'} > > > def __init__( self, label ): > > """ > > @param > > """ > > self.label = label > > > def __repr__(self): > > """ > > @param self > > """ > > className = self.__class__.__name__ > > return "<%s ('%s')>" % ( className, self.label) > > > #------------------------------------------------------------------------------- > > > from sqlalchemy.ext.declarative import DeclarativeMeta > > > def add_a_table(tablename): > > return DeclarativeMeta("%sMyBase"%tablename, (BaseTest,), > > {'__tablename__':tablename}) > > > add_a_table('part_uk') > > > engine = create_engine( 'mysql://root:dud...@127.0.0.1:3306/test', > > echo = True ) > > > Session = sessionmaker( bind = engine, > > autoflush=True, > > autocommit=True ) > > session = Session() > > > BaseTest.metadata.drop_all(engine) > > BaseTest.metadata.create_all(engine) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---