Skip to content

Instantly share code, notes, and snippets.

@Kd-Here
Created March 4, 2023 19:57
Show Gist options
  • Save Kd-Here/015f9df029fe99e0f4707ea12ee1b357 to your computer and use it in GitHub Desktop.
Save Kd-Here/015f9df029fe99e0f4707ea12ee1b357 to your computer and use it in GitHub Desktop.
Relationship shown
from . import db #i.e. same from website import db ( . means current and we had __init__.py which is used for package )
from flask_login import UserMixin
from sqlalchemy.sql import func
# This is schema for user made notes
class Note(db.Model):
"""
We storing user notes which will have userNote & datetime when it was created
foreign key is used to establish a connection between note and user database which user created which note.
"""
id = db.Column(db.Integer, primary_key = True)
userNote = db.Column(db.String(10000))
date = db.Column(db.DateTime(timezone=True),default=func.now())
user_id = db.Column(db.Integer,db.ForeignKey('user.id'))
"""In python class are Upper first letter but in database all are lower case thus User becomes user
One user has many notes i.e One to many releation. Thus every time we
have note we will know which user has created it by checking user_id"""
# This is schema for User details
class User(db.Model,UserMixin):
"""
User is database storing all details of user so we have class called user that stores schema,
UserMixin is used bcoz we already used login package of flask that helps in creating login application so for storing we used Mixin
"""
id = db.Column(db.Integer,primary_key = True)
email = db.Column(db.String(150),unique = True)
password = db.Column(db.String(150))
first_name = db.Column(db.String(150))
notes = db.relationship("Note")
"""Like Foreign key we don't need to put Note here in lower bcoz sqlalchemy is
following different logic for relationship"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment