So I have the following situation. I have a class DataTypes which has the following structure:
class DataType(Base):
__tablename__ = 'DATA_TYPES'
id = Column(Integer, primary_key=True)
type_name = Column(String)
fk_result_storage = Column(Integer, ForeignKey('DATA_STORAGES.id'))
parentDataStorage = relationship("DataStorage", backref=backref("DataType", cascade="all,delete"))
def __init__(self, name, resultId):
self.type_name = name
self.fk_result_storage = resultId
Now the relationship defined here works. But now I have some specific data types that are created dynamically trough introspection and that need to be deleted cascaded also. They are created like this:
t = Table('DATA_' + obj.__name__.lower(), *t[:-1], **t[-1])
mapper(obj, t, *args, **kwargs)
model.Base.metadata.create_all(dao.Engine)
This works fine and tables are created as needed. But now I want to add a relationship
similar to the one from DataType class. So I tried this:
t = T('DATA_' + obj.__name__.lower(), *t[:-1], **t[-1])
M(obj,t,properties = {'children' : relationship('DataType', backref=backref(obj, cascade="all,delete"))} )
model.Base.metadata.create_all(dao.Engine)
But this gives me:
sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Original exception was: relationship 'children' expects a class or a mapper argument (received: <type 'str'>)
I`m quite new to SQLAlchemy. Any suggestions ?
Regards,
Bogdan
SQLAlchemy is not my strongest skill, but I think this property is wrong:
I think this should be:
I.E., the reference to the DataType is the class, not a string.