Skip to content

Instantly share code, notes, and snippets.

@maxtortime
Created August 8, 2016 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxtortime/ae48d7e0fa0dbd8deda1004bdec9983c to your computer and use it in GitHub Desktop.
Save maxtortime/ae48d7e0fa0dbd8deda1004bdec9983c to your computer and use it in GitHub Desktop.
flask-sqlalchemy 외래키 구현
# Define models
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
# Role table
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))
# User's table
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic'))
@maxtortime
Copy link
Author

maxtortime commented Aug 8, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment