I am trying to generate tables/classes dynamically. The code below is
my latest attempt, but I cannot get it to work.

-------------------------------------------------------------
 class TableName(object):
    @declared_attr
    def __tablename__(cls): return cls.__name__

class Inherit(object):
    @declared_attr
    def id(cls): #<= is not called for S
        base = cls.__bases__[len(cls.__bases__) - 1]
        print "class, base:", cls.__name__, base.__name__
        return Column(Integer, ForeignKey(base.__name__ + '.id'),
primary_key = True)
    @declared_attr
    def __mapper_args__(cls):
        return {'polymorphic_identity': cls.__name__}

class Object(Base, TableName):

    association_tables = {}

    id = Column(Integer, primary_key = True)
    type_name = Column(String(50),  nullable = False)
    __mapper_args__ = {'polymorphic_on': type_name}



if __name__ == '__main__':
    session = setup(engine)

    T = type('T', (Inherit, Object), {'Tdata': Column(String(50))})
    S = type('S', (T,), {'Sdata': Column(String(50))}) #<= Error
    session.commit()
    print S.__table__.c
-------------------------------------------------------------
the output is:
-------------------------------------------------------------
class, base: T Object
class, base: T Object
class, base: T Object
Traceback (most recent call last):
  File "D:\Documents\Code\Eclipse\workspace\SQLAdata\src\test4.py",
line 55, in <module>
    S = type('S', (T,), {'Sdata': Column(String(50))})
  File "C:\Python27\lib\site-packages\sqlalchemy\ext\declarative.py",
line 1336, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "C:\Python27\lib\site-packages\sqlalchemy\ext\declarative.py",
line 1329, in _as_declarative
    **mapper_args)
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\__init__.py",
line 1116, in mapper
    return Mapper(class_, local_table, *args, **params)
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\mapper.py", line
197, in __init__
    self._configure_inheritance()
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\mapper.py", line
473, in _configure_inheritance
    self.local_table)
  File "C:\Python27\lib\site-packages\sqlalchemy\sql\util.py", line
303, in join_condition
    "between '%s' and '%s'.%s" % (a.description, b.description, hint))
sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships
between 'T' and 'S'.
-------------------------------------------------------------
What is wrong with this approach. Is there a good way to approach this
problem (I have tried a couple already).

Also: Why is Inherit.id() called 3 times for T

Please help!

Lars

-- 
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