From 850e780fd065b53952aa979599dd6980010c1172 Mon Sep 17 00:00:00 2001 From: erichard <elliot.richard@lal.in2p3.fr> Date: Fri, 21 Feb 2020 17:21:04 +0100 Subject: [PATCH] Created models for the database. --- models.py | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/models.py b/models.py index 791b7dd..d51e759 100644 --- a/models.py +++ b/models.py @@ -1,15 +1,42 @@ from flask import Flask from flask_sqlalchemy import SQLAlchemy -db = SQLAlchemy() +app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' +db = SQLAlchemy(app) -class Element(db.Model) +element_label = db.Table('Element_Label', db.Model.metadata, + db.Column('id_element', db.Integer, db.ForeignKey('element.id')), + db.Column('id_label', db.Integer, db.ForeignKey('label.id')) + ) + +class Element(db.Model): id = db.Column(db.INTEGER, primary_key=True) element_name = db.Column(db.String(50), unique=True, nullable=False) + labels = db.relationship( + "Label", + secondary=element_label, + back_populates="elements") -class Label(db.Model) +class Label(db.Model): id = db.Column(db.INTEGER, primary_key=True) label_name = db.Column(db.String(50), unique=True, nullable=False) father_id = db.Column(db.INTEGER, db.ForeignKey('label.id')) + children = db.relationship("Label", + backref=db.backref('parent', remote_side=[id]) + ) + aliases = db.relationship('Alias', back_populates='label') + elements = db.relationship( + "Element", + secondary=element_label, + back_populates="labels") + +class Alias(db.Model): + alias_name = db.Column(db.String(100), primary_key=True) + id_label = db.Column(db.INTEGER, db.ForeignKey('label.id'), nullable=False) + label = db.relationship('Label', back_populates='aliases') + + + -- GitLab