> -----Original Message----- > From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] > On Behalf Of Cody Django > Sent: 20 June 2011 19:37 > To: sqlalchemy > Subject: [sqlalchemy] dynamically set table_name at runtime > > Hello! > > I would like to dynamically set/change the table that is mapped in my > python object, as instantiated through the declarative style. > > > class Feature(Base, GeometryTableMixIn): > """ this is dynamically created to use a table and pk_column > determined at runtime """ > > __table_args__ = { > "schema": 'a_schema', > "autoload": True, > "autoload_with": Session.bind, > "useexisting": True > } > > wkb_geometry = GeometryColumn('wkb_geometry', Geometry(srid=4269)) > > def __init__(self, *args, **kwargs): > self.__tablename__ = kwargs['tablename'] > self.pk_id = Column('%s' % kwargs['pk_id'], types.Integer, > primary_key=True, autoincrement=False) > super(Feature, self).__init__(*args, **kwargs) > > > > > This doesn't work: > > InvalidRequestError: Class <class 'javelin.model.feature.Feature'> > does not have a __table__ or __tablename__ specified and does not > inherit from an existing table-mapped class. > > > > Could this possibly be done through another approach? Suggestions > are > greatly appreciated. >
Can you describe your use case? The solution presented in the StackOverflow article seems like a hack at best. A cleaner way to do the same thing might be: def make_feature_class(tablename): class Feature(Base, GeometryTableMixIn): __table__ = tablename # etc. return Feature ...but the whole thing feels strange. What are you actually trying to do? Cheers, Simon -- 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.