Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
Python :: updating lists 
Python :: from django.urls import path, re_path 
Python :: python show difference between two strings and colorize it 
Python :: python csv file plot column 
Python :: python print list of keywords 
Python :: Example of inheritance and constructor in subclass 
Python :: metros para cm para mm 
Python :: Generating variations on image data 
Python :: While Loop Python Range Staying Constant Despite Shrinking List 
Python :: multiplying float variables python and print 
Python :: python how to tell if class is initialized 
Python :: list comperhension condition in python 
Python :: import knn in python jupyter 
Python :: django check if related object is None 
Python :: python Find Hash 
Python :: how to run another python file in python 
Python :: Django forms I cannot save picture file 
Python :: signup generic 
Python :: transfer sound to hz with python 
Python :: how to create a scoreboard for the top 5 players in python 
Python :: sqlite to python list 
Python :: sklearn cheat sheet 
Python :: how to place an id to every element in list in python 
Python :: even number list generator 
Python :: insert python 
Python :: flask logging miguel grinberg 
Python :: html to image pygame python 
Python :: create new column with first character of string pyspark 
Python :: InvalidArgumentError: logits and labels must be broadcastable: logits_size=[16,3] labels_size=[16,2] [[node categorical_smooth_loss/softmax_cross_entropy_with_logits 
Python :: child urls python 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =