class HasSomeAttribute(object):
@declared_attr.cascading
def type(cls): if has_inherited_table(cls): if cls.__name__ == 'MySubClass1': return db.Column(db.Integer, default=1) else: return db.Column(db.Integer, default=2) else: return db.Column(db.Integer, default=0) class MyClass(HasSomeAttribute, db.Model): __tablename__ = 'people4l2' id = db.Column(db.Integer, primary_key=True)
class MySubClass1(MyClass): pass class MySubClass2(MyClass): pass
ArgumentError: Column 'type' on class <class '__main__.MySubClass1'> conflicts with existing column 'people4l2.type'class HasSomeAttribute(object):
@declared_attr.cascading
def type(cls): if has_inherited_table(cls): if cls.__name__ == 'MySubClass1': return db.Column(db.Integer, default=1) else: return db.Column(db.Integer, default=2) else: return db.Column(db.Integer, default=0) class MyClass(HasSomeAttribute, db.Model): __tablename__ = 'people4l2' id = db.Column(db.Integer, primary_key=True)
id1 = db.Column(db.Integer)
id2 = db.Column(db.Integer)
class MySubClass1(MyClass): pass class MySubClass2(MyClass): pass
class Right(db.Model):
id = db.Column(db.Integer, primary_key=True)
subclass_attr = relationship( 'Contact', secondary= MySubClass1.__table__, primaryjoin='and_(MySubClass1.type == 802, MySubClass1.id2 == Right.id)', secondaryjoin='and_(MySubClass1.type == 802, MySubClass1.id1 == Left.id)' )
class Left(db.Model):
id = db.Column(db.Integer, primary_key=True)class HasSomeAttribute(object):
@declared_attr.cascading
def type(cls): if has_inherited_table(cls): if cls.__name__ == 'MySubClass1': return db.Column(db.Integer, default=1) else: return db.Column(db.Integer, default=2) else: return db.Column(db.Integer, default=0) class MyClass(HasSomeAttribute, db.Model): __tablename__ = 'people4l2' id = db.Column(db.Integer, primary_key=True)
id1 = db.Column(db.Integer)
id2 = db.Column(db.Integer)
class MySubClass1(MyClass): pass class MySubClass2(MyClass): pass
class Right(db.Model):
id = db.Column(db.Integer, primary_key=True)
left = relationship( 'Left', secondary= MySubClass1.__table__, primaryjoin='and_(MySubClass1.type == 802, MySubClass1.id2 == Right.id)', secondaryjoin='and_(MySubClass1.type == 802, MySubClass1.id1 == Left.id)' )
class Left(db.Model):
id = db.Column(db.Integer, primary_key=True)right1 = Right()
right.left = Left()
right2 = Right2()
right2.left = Left2()
db.session.add(right) // automatically create the junction using MySubClass1 and set the type field to 1
db.session.add(right2) // automatically create the junction using MySubClass1 and set the type field to 2
db.session.commit()class MyClass(HasSomeAttribute, db.Model): __tablename__ = 'people4l2' id = db.Column(db.Integer, primary_key=True)
class MySubClass1(MyClass):Unfortunately I'm inheriting the relational model from an old application. I have dozens of tables using a single junction table for associations.I can not completely redesign my relational model because it needs to be compatible with the old application.
There are no foreign key constraints in the database schema, id1 and id2 are just stored there, a type column is used to retrieve records e.g type equals B for an association between RightB and ReftB and equals A between RightA and LeftA. That is why I'm trying to set a default value for the type column so I don't have to deal with that junction table when inserting records.
Here's a simple visual of the schema
...