Re: [sqlalchemy] Repetitive Fields in declarative
On Fri, Aug 19, 2011 at 9:23 AM, Mark Erbaugh wrote: > > On Aug 19, 2011, at 10:41 AM, Michael Bayer wrote: > > > Id use a mixin so that a superclass can be generated in a data driven > manner: > > > > > > MyCols = type("MyCols", (object, ), dict(("field%d" % i, Column(Integer)) > for i in xrange(1, 10))) > If you are going to do this a lot, it can be a pain with declarative. I developed a framework that I used, based on ORM, which uses an explicit _makeTable() and _mapTable() objects that I call in order to create and map the tables. Since these are python methods, I can use any kind of python iteration or code I want to decide what columns to create. It is a pretty flexible model. Example conceptual code: class Database(object): table_args = { 'mysql_engine' : 'InnoDB' } schema = "database_name" def __init__(self,dbclasses=[]): self.metadata = MetaData() self.engine = ... (set up engine, etc.) self.dbclasses = dbclasses for c in self.dbclasses: c._makeTable(self,self.engine) cls.__table__.create(bind=self.engine,checkfirst=True) for c in self.dbclasses: c._mapTable(self) class FooRecord(object): @classmethod def _makeTable(cls,db,engine): cls.db = db cls.__table__ = Table('foo', db.metadata, Column('x'), Column('y'), **cls.table_args, schema=cls.schema etc.) @classmethod def _mapTable(cls,db): mapper(cls, cls.__table__, properties={ ... }) db = Database([FooRecord]) You may find a model like this easier to use to create dynamically-generated tables. The point here is that SQLAlchemy is sufficiently flexible so that if declarative doesn't meet your needs or is a bit cumbersome for what you want to do, you can just start at the ORM (below declarative) level and build up a framework that works for you. Regards, Daniel -- 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] Repetitive Fields in declarative
On Aug 19, 2011, at 10:41 AM, Michael Bayer wrote: > Id use a mixin so that a superclass can be generated in a data driven manner: > > > MyCols = type("MyCols", (object, ), dict(("field%d" % i, Column(Integer)) for > i in xrange(1, 10))) > > class MyClass(MyCols, Base): >... > > otherwise if you want to go the append_column() route, you can just tack them > on the class, declarative will call append_column() as well as > mapper.add_property(): > > for name, col in ("field%d" % i, Column(Integer)) for i in xrange(1, 10)): >setattr(MyClass, name, col) Michael, Thanks for the info. Mark -- 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] Repetitive Fields in declarative
On Aug 18, 2011, at 6:06 PM, Mark Erbaugh wrote: > I want to create a table that has several similar fields. For example, assume > the fields are field1, field2, ... > > Is there a way in the declarative class that I can do something like: > > for i in range(10): > 'field%d' % i = Column( ... ) > Id use a mixin so that a superclass can be generated in a data driven manner: MyCols = type("MyCols", (object, ), dict(("field%d" % i, Column(Integer)) for i in xrange(1, 10))) class MyClass(MyCols, Base): ... otherwise if you want to go the append_column() route, you can just tack them on the class, declarative will call append_column() as well as mapper.add_property(): for name, col in ("field%d" % i, Column(Integer)) for i in xrange(1, 10)): setattr(MyClass, name, col) -- 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] Repetitive Fields in declarative
On Aug 18, 2011, at 7:01 PM, Mark Erbaugh wrote: > > On Aug 18, 2011, at 6:06 PM, Mark Erbaugh wrote: > >> want to create a table that has several similar fields. For example, assume >> the fields are field1, field2, ... >> >> Is there a way in the declarative class that I can do something like: >> >> for i in range(10): >> 'field%d' % i = Column( ... ) >> >> >> Thanks, >> Mark > > > Figured it out: > > after the class definition: > > for i in range(10): > .__table__.append_column(Column('field%d' % i, ...)) I guess not: while the above code adds the fields to the database table, it doesn't add them as named data members of the class. Here's my latest effort: class Preferences: ... for i in range(10): setattr(Preferences, 'field%d' % i, Column(... This also answers my question about relationships setattr(Preferences 'relationship%d' % i, relationship(... Mark -- 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] Repetitive Fields in declarative
Me again (see below): On Aug 18, 2011, at 7:01 PM, Mark Erbaugh wrote: > > On Aug 18, 2011, at 6:06 PM, Mark Erbaugh wrote: > >> want to create a table that has several similar fields. For example, assume >> the fields are field1, field2, ... >> >> Is there a way in the declarative class that I can do something like: >> >> for i in range(10): >> 'field%d' % i = Column( ... ) >> >> >> Thanks, >> Mark > > > Figured it out: > > after the class definition: > > for i in range(10): > .__table__.append_column(Column('field%d' % i, ...)) Some of the fields that I am adding this way are foreign keys to another table. Is there a way to specify a relationship based on these foreign key fields? Mark -- 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] Repetitive Fields in declarative
On Aug 18, 2011, at 6:06 PM, Mark Erbaugh wrote: > want to create a table that has several similar fields. For example, assume > the fields are field1, field2, ... > > Is there a way in the declarative class that I can do something like: > > for i in range(10): > 'field%d' % i = Column( ... ) > > > Thanks, > Mark Figured it out: after the class definition: for i in range(10): .__table__.append_column(Column('field%d' % i, ...)) Mark -- 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.
[sqlalchemy] Repetitive Fields in declarative
I want to create a table that has several similar fields. For example, assume the fields are field1, field2, ... Is there a way in the declarative class that I can do something like: for i in range(10): 'field%d' % i = Column( ... ) Thanks, Mark -- 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.