From 454728d5ce66d7d5daff927f0c463a3685c162e8 Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Tue, 27 Apr 2021 19:25:52 +0200 Subject: [PATCH] Use sqlalchemy relationship --- app/commands/commands.py | 6 +++--- app/models.py | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/commands/commands.py b/app/commands/commands.py index 46d1998..b8c5216 100644 --- a/app/commands/commands.py +++ b/app/commands/commands.py @@ -200,10 +200,10 @@ def feed_from_irap(csv_file_name): print("Adding category ", c) n_c = Category(name=c) db.session.add(n_c) - db.session.commit() + # db.session.commit() for label in l: - print("Adding label {} for id {}".format(label, n_c.id)) - n_l = CategoryLabel(name=label, category_id=n_c.id) + # print("Adding label {} for id {}".format(label, n_c.id)) + n_l = CategoryLabel(name=label, category=n_c) db.session.add(n_l) db.session.commit() diff --git a/app/models.py b/app/models.py index d1443d7..5655d5f 100644 --- a/app/models.py +++ b/app/models.py @@ -1,4 +1,5 @@ from flask_sqlalchemy import SQLAlchemy +from sqlalchemy.orm import relationship db = SQLAlchemy() @@ -6,12 +7,14 @@ db = SQLAlchemy() class Category(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(32)) + labels = relationship("CategoryLabel", back_populates='category') class CategoryLabel(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(32)) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) + category = relationship("Category", back_populates='labels') class AgentBap(db.Model): -- libgit2 0.21.2