Search
 
SCRIPT & CODE EXAMPLE
 

SQL

flask sqlalchemy single table inheritance

class User(db.Model):
    __tablename__ = 'tbl_user'
    type = db.Column(db.String(32)) 
    ...
    __mapper_args__ = {
        'polymorphic_identity': 'user',
        'polymorphic_on': type,
    }  # remove with_polymorphic

class Tourist(User):
    __tablename__ = None  # Add table name to be None
    __mapper_args__ = {
        'polymorphic_identity': 'tourist'
    }
    ...

class Guide(User):
    __tablename__ = None  # Add table name to be None
    __mapper_args__ = {
        'polymorphic_identity': 'guide'
    }
    ...
Comment

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()
Comment

PREVIOUS NEXT
Code Example
Sql :: What is the difference between the LIKE and REGEXP operators in mysql? 
Sql :: show database size or specific database table size 
Sql :: mysql BEFORE UPDATE INSERT 
Sql :: postgresql display subquery as json 
Sql :: ms sql filter all sympbol 
Sql :: sqlites studio red exclamation mark when poening databse 
Sql :: ORA-01400 
Sql :: acutal month year 
Sql :: liquibase default-schema in sql 
Sql :: window function to forward fill 
Sql :: how to merge to coloumns into a single column with a space. 
Sql :: how to make sure two tables have same exact data in sql 
Sql :: what is the difference between an embedded database and a normal 
Sql :: create trigger in phpmyadmin 
Sql :: homebrew/sqlitestudio 
Sql :: SQL-Arten 
Sql :: clickhouse greatest non-aggregate 
Sql :: sql case when exists in another table 
Sql :: postgresql get tables where column is foreign key 
Sql :: nosql databases 
Sql :: correlated subquery 
Sql :: create table database in psql 
Sql :: sql first day quarter 
Sql :: add 10 to all numbers in a column sql 
Sql :: stored procedure to change name of column for all dependent tables and views 
Csharp :: unity get number of child objects 
Csharp :: unity how to convert mouse screen position to world position 
Csharp :: c# writeline debug 
Csharp :: c# open folder in explorer 
Csharp :: c# center text 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =