I would like to have a model class that inherits from 2 other model classes. Here is something that should work but it doesnt.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
class BaseModel(db.Model):
__abstract__ = True
id = db.Column(db.Integer, primary_key=True)
class A(BaseModel):
a_name = db.Column(db.String)
a_type = db.Column(db.String)
__mapper_args__ = {
'polymorphic_on': a_type,
'polymorphic_identity': 'a',
}
class B(BaseModel):
b_name = db.Column(db.String)
b_type = db.Column(db.String)
__mapper_args__ = {
'polymorphic_identity': 'b',
'polymorphic_on': b_type
}
class Inheritance(A, B):
a_id = db.Column(db.ForeignKey(A.id), primary_key=True)
b_id = db.Column(db.ForeignKey(B.id), primary_key=True)
name = db.Column(db.String)
db.create_all()
db.session.add_all((A(), B()))
db.session.commit()
db.session.add(Inheritance(a_id=1, b_id=1))
db.session.commit()
I got this error
sqlalchemy.exc.InvalidRequestError: Class <class '__main__.Inheritance'> has multiple mapped bases: [<class '__main__.A'>, <class '__main__.B'>]
What is wrong? I understand what the error says, but I dont know how to fix it.
Can anyone give me a minimal but fully working example of how to set it up?