简体   繁体   中英

Flask-SQLAlchemy association table

I make a tuition payment app using Flask and using Flask-SQLAlchemy for ORM.

I have a User table, and this table I'm using to log in for school account and parent account. While the student data I store it in table Student and the student don't have a login account.

What I want to ask for, how to make relationship for the Student table to SCOOL_ID and PARENT_ID ..? while the parent account and the school account is in the same table, which is User table.

One student have one parent and one student have one school.

Did I must separate the table between the school and the parent..?

So far, I have made it with Flask-SQLAlchemy like this, but I'm not sure this good.

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(200))
    email = db.Column(db.String(120), index=True, unique=True)
    password = db.Column(db.String(255))
    active = db.Column(db.Boolean())
    created_at = db.Column(db.DateTime, default=datetime.utcnow())
    updated_at = db.Column(db.DateTime, default=datetime.utcnow())


association_roles = db.Table('association_roles',
    db.Column('parent_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('school_id', db.Integer, db.ForeignKey('user.id'))
)

class Student(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    student_registration_number = db.Column(db.String(120), index=True, unique=True)
    name = db.Column(db.String(100))
    major = db.Column(db.String(50))
    created_at = db.Column(db.DateTime, default=datetime.utcnow())
    updated_at = db.Column(db.DateTime, default=datetime.utcnow())

    association_roles = db.relationship(
        'Student', secondary=association_roles,
        primaryjoin=(association_roles.c.parent_id == id),
        secondaryjoin=(association_roles.c.school_id == id),
        backref=db.backref('student', lazy='dynamic'), lazy='dynamic')

So, how to do that..?, any help will be much appreciated.

I already know the concept for my question, I must make a table School and Parent and inherit it from the table User , here is snippet of the code:

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(200))
    roles = db.relationship('Role', secondary=roles_users,
                            default='parent',
                            backref=db.backref('users', lazy='dynamic'))

    __mapper_args__ = {
        'polymorphic_identity': 'user',
        'with_polymorphic': '*'
    }

class Parent(User):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    school_id = db.Column(db.Integer, db.ForeignKey('school.id'))

    __mapper_args__ = {
        'polymorphic_identity': 'parent',
        'with_polymorphic': '*'
    }


class School(User):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    __mapper_args__ = {
        'polymorphic_identity': 'school',
        'with_polymorphic': '*'
    }


class Student(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    school_id = db.Column(db.Integer, db.ForeignKey('school.id'))
    parent_id = db.Column(db.Integer, db.ForeignKey('parent.id'))
    parent = db.relationship('Parent', backref=db.backref('Student', lazy='dynamic'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM