class Book(db.Model): __tablename__ = 'book' id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String) author_id = db.Column(db.Integer, db.ForeignKey('author.id')) genre_id = db.Column(db.Integer, db.ForeignKey('genre.id')) characters = db.relationship('Character', secondary=books_char_relation, backref=db.backref('characters'), lazy='dynamic')
class Name(db.Model): __tablename__ = 'name' id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String) persons = db.relationship('Person', backref='carrier') def __init__(self, name): self.name = name
class Person(db.Model): __tablename__ = 'person' id = db.Column(db.Integer, autoincrement=True, primary_key=True) age = db.Column(db.Integer) carrier_id = db.Column(db.Integer, db.ForeignKey('name.id')) def __init__(self, age, name_id): self.age = age self.name_id = name_id
class UserTable(db.Model): __tablename__ = 'user_table' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) age = db.Column(db.INTEGER) address = db.Column(db.String(120)) def __init__(self, name, age, address): self.name = name self.age = age self.address = address
class TestTable(db.Model): __tablename__ = 'test_table' id = db.Column(db.Integer, autoincrement=True, primary_key=True) field = db.Column(db.String)
class Character(db.Model): __tablename__ = 'character' id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String)
class IdCard(db.Model): __tablename__ = 'id_card' id = db.Column(db.Integer, autoincrement=True, primary_key=True) count_of_books = db.Column(db.Integer) user_id = db.Column(db.Integer, db.ForeignKey('user_table.id')) user = db.relationship('User', backref=db.backref('user'))
class Genre(db.Model): __tablename__ = 'genre' id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String) books = db.relationship('Book', backref='genre')
class Author(db.Model): __tablename__ = 'author' id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String) books = db.relationship('Book', backref='author') city = db.Column(db.String)
class User(db.Model): __tablename__ = 'user_table' id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String) books = db.relationship('Book', secondary=user_books_relation, backref=db.backref('users'), lazy='dynamic')
from service_api.app_database import db # ======================= Many to Many Relationship ======================= # books_char_relation = db.Table('books_char_relation', db.Column('book_id', db.Integer, db.ForeignKey('book.id')), db.Column('character_id', db.Integer, db.ForeignKey('character.id')) ) class Character(db.Model): __tablename__ = 'character' id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String) user_books_relation = db.Table('user_books_ralation', db.Column('user_id', db.Integer, db.ForeignKey('user_table.id')), db.Column('book_id', db.Integer, db.ForeignKey('book.id')) ) class User(db.Model): __tablename__ = 'user_table' id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String) books = db.relationship('Book', secondary=user_books_relation, backref=db.backref('users'), lazy='dynamic') # ======================= One to Many Relationship ======================= # class Author(db.Model):