Skip to content

Instantly share code, notes, and snippets.

@xuru
Created January 5, 2016 21:16
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 xuru/6cf4384ecb846f22019b to your computer and use it in GitHub Desktop.
Save xuru/6cf4384ecb846f22019b to your computer and use it in GitHub Desktop.
one to one mapping sqlalchemy
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import validates
db = SQLAlchemy()
class User(db.Model):
__tablename__ = "user"
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.Unicode)
last_name = db.Column(db.Unicode)
email = db.Column(db.Unicode, nullable=True, unique = True)
phone = db.Column(db.Unicode, nullable=True, unique = True)
profile_pic = db.Column(db.Text, default='')
loyalty = relationship("Loyalty", uselist=False, back_populates="user")
@validates('email')
def validate_email(self, key, email):
assert '@' in email
return email
class Loyalty(db.Model):
__tablename__ = "loyalty"
id = db.Column(db.Integer, primary_key=True)
card_number = db.Column(db.Integer)
points = db.Column(db.Integer)
balance = db.Column(db.Integer)
group_id = db.Column(db.Integer)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
user = relationship("Loyalty", back_populates="loyalty")
db.create_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment