class User(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(100), nullable=False, unique=True) firstName = db.Column(db.String(64), nullable=True) lastName = db.Column(db.String(64), nullable=True) # delete routes in case the parent table item (user) is deleted routes = db.relationship("Route", cascade="delete", back_populates="user") @staticmethod def get_schema(): schema = {"type": "object", "required": ["email"]} props = schema["properties"] = {} props["email"] = { "description": "users unique email address", "type": "string" } props["firstName"] = { "description": "users first name", "type": "string" } props["lastName"] = { "description": "users last name", "type": "string" } return schema
class Discipline(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), nullable=False, unique=True, default="Bouldering") routes = db.relationship("Route", back_populates="discipline")
class Location(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False, unique=True, default="Oulun Kiipeilykeskus") routes = db.relationship("Route", back_populates="location")
class Route(db.Model): id = db.Column(db.Integer, primary_key=True) # when user is deleted, also routes are deleted userId = db.Column(db.Integer, db.ForeignKey("user.id", ondelete="CASCADE"), nullable=False) date = db.Column(db.Date, nullable=False) # if location, discipline, or grade is deleted we want to keep record of the route still, # so we set these values to NULL in case of deletion in the parent table locationId = db.Column(db.Integer, db.ForeignKey("location.id", ondelete="SET NULL")) disciplineId = db.Column( db.Integer, db.ForeignKey("discipline.id", ondelete="SET NULL")) gradeId = db.Column(db.Integer, db.ForeignKey("grade.id", ondelete="SET NULL")) extraInfo = db.Column(db.String(250), nullable=True) user = db.relationship("User", back_populates="routes") location = db.relationship("Location", back_populates="routes") discipline = db.relationship("Discipline", back_populates="routes") grade = db.relationship("Grade", back_populates="routes") @staticmethod def get_schema(): schema = { "type": "object", "required": ["date", "location", "discipline", "grade"] } props = schema["properties"] = {} props["date"] = { "description": "date when the route was climbed", "type": "string" } props["lastName"] = { "location": "location where the route was climbed", "type": "string" } props["discipline"] = { "discipline": "discipline of the route", "type": "string" } props["grade"] = {"grade": "grade of the route", "type": "string"} return schema
class Grade(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(10), nullable=False, unique=True) routes = db.relationship("Route", back_populates="grade")