Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

flask-sqlalchemy inheritance

import sqlalchemy as sa
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 = sa.Column(sa.Integer, primary_key=True)


class A(BaseModel):
    a_name = sa.Column(sa.String)
    a_type = sa.Column(sa.String)
    __mapper_args__ = {
        'polymorphic_on': a_type,
        'polymorphic_identity': 'a',
    }


class B(BaseModel):
    b_name = sa.Column(sa.String)
    b_type = sa.Column(sa.String)
    __mapper_args__ = {
        'polymorphic_identity': 'b',
        'polymorphic_on': b_type
    }


class Inheritance(A, B):
    a_id = sa.Column(sa.ForeignKey(A.id), primary_key=True)
    b_id = sa.Column(sa.ForeignKey(B.id), primary_key=True)
    name = sa.Column(sa.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()
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #inheritance
ADD COMMENT
Topic
Name
1+9 =