From 44ca03d7dc1a25ee9c0e003d615de5bd65587c5e Mon Sep 17 00:00:00 2001 From: Peery Date: Tue, 14 Feb 2023 18:01:08 +0100 Subject: [PATCH] Changed references for Table "Tag" The column "tag_id" was renamed "id" in the database schema. Code references have been updated to reflect this change where necessary and where helpful. --- database/database.py | 30 ++++++++++++++++-------------- database/models.py | 17 ++++++++++++++++- main.py | 4 ++-- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/database/database.py b/database/database.py index bb718c5..d3eac94 100644 --- a/database/database.py +++ b/database/database.py @@ -18,9 +18,9 @@ Base = sqlalchemy.orm.declarative_base() art_to_tag_table = Table('art_to_tag', Base.metadata, Column('art_id', Integer, ForeignKey('art.id')), - Column('tag_id', Integer, ForeignKey('tag.tag_id'))) + Column('tag_id', Integer, ForeignKey('tag.id'))) -art_to_art_collection = Table('art_to_art_collection', Base.metadata, +"""art_to_art_collection = Table('art_to_art_collection', Base.metadata, Column('collection_id', Integer, ForeignKey('art_collection.id')), Column('art_id', Integer, ForeignKey('art.id')), Column('ranking', String))""" @@ -33,15 +33,15 @@ artist_to_topic_table = Table('artist_to_topic', Base.metadata, class DBTagImplication(Base): __tablename__ = 'tag_implication' - root_tag = Column(Integer, ForeignKey('tag.tag_id'), primary_key=True) - implicate = Column(Integer, ForeignKey('tag.tag_id'), primary_key=True) + root_tag = Column(Integer, ForeignKey('tag.id'), primary_key=True) + implicate = Column(Integer, ForeignKey('tag.id'), primary_key=True) class DBTagAlias(Base): __tablename__ = 'tag_alias' - tag1 = Column(Integer, ForeignKey('tag.tag_id'), primary_key=True) - tag2 = Column(Integer, ForeignKey('tag.tag_id'), primary_key=True) + tag1 = Column(Integer, ForeignKey('tag.id'), primary_key=True) + tag2 = Column(Integer, ForeignKey('tag.id'), primary_key=True) class DBPresence(Base): @@ -131,13 +131,13 @@ class DBTagCategory(Base): category_id = Column(Integer, primary_key=True) name = Column(String(20), nullable=False) - tags = relationship("DBTag", back_populates="category", cascade="all, delete") + tags = relationship("DBTag", back_populates="category", cascade="all, delete, delete-orphan") class DBTag(Base): __tablename__ = "tag" - tag_id = Column(Integer, primary_key=True) + id = Column(Integer, primary_key=True) name = Column(String(50), unique=True) description = Column(String) category_id = Column(Integer, ForeignKey('tag_category.category_id')) @@ -147,9 +147,11 @@ class DBTag(Base): art = relationship("DBArt", secondary=art_to_tag_table, back_populates="tags") # TODO check if cascade is required implications = relationship(DBTagImplication, backref="implied_by", - primaryjoin=tag_id == DBTagImplication.root_tag) + primaryjoin=id == DBTagImplication.root_tag, + cascade="all, delete, delete-orphan") alias = relationship(DBTagAlias, backref="alias", - primaryjoin=(tag_id == DBTagAlias.tag1) or (tag_id == DBTagAlias.tag2)) + primaryjoin=(id == DBTagAlias.tag1) or (id == DBTagAlias.tag2), + cascade="all, delete, delete-orphan") Base.metadata.create_all(bind=engine) @@ -453,7 +455,7 @@ class Database: return self.__get_db().query(DBTag).all() # TODO fix StackOverflow def get_tag_by_id(self, tag_id: int) -> DBTag: - result = self.__get_db().query(DBTag).where(tag_id == DBTag.tag_id).first() + result = self.__get_db().query(DBTag).where(tag_id == DBTag.id).first() return result def get_tag_by_name(self, name: str) -> DBTag: @@ -485,7 +487,7 @@ class Database: return db_tag def update_tag_by_model(self, tag: Tag): - db_tag = self.get_tag_by_id(tag.tag_id) + db_tag = self.get_tag_by_id(tag.id) db_categ = self.get_category_by_id(tag.category_id) if db_categ is None: @@ -520,13 +522,13 @@ class Database: def search_tag_by_name_fuzzy(self, search: str) -> list: # return a list of tags fitting the fuzzy name search result = self.__get_db().query(DBTag).filter(DBTag.name.ilike("%{}%".format(search))) \ - .options(load_only("tag_id", "name")).all() + .options(load_only("id", "name")).all() return result # Tag -> Art def get_tag_art(self, tag_id: int): - result = self.__get_db().query(DBArt).filter(DBArt.tags.any(tag_id=tag_id)).all() + result = self.__get_db().query(DBArt).filter(DBArt.tags.any(id=tag_id)).all() return result def update_tag_art(self, tag_id: int): # is this useful? diff --git a/database/models.py b/database/models.py index aec1082..8f5a94e 100644 --- a/database/models.py +++ b/database/models.py @@ -66,7 +66,22 @@ class TagNoID(BaseModel): class Tag(TagNoID): - tag_id: int + id: int + + class Config: + orm_mode = True + + +class CollectionNoID(BaseModel): + name: Optional[str] + description: Optional[str] + + class Config: + orm_mode = True + + +class Collection(CollectionNoID): + id: int class Config: orm_mode = True diff --git a/main.py b/main.py index a6671bb..b6be0d7 100644 --- a/main.py +++ b/main.py @@ -287,10 +287,10 @@ async def tag(tag: TagNoID, id: int = None): print(f"Received POST on /artnet/metadata/tag (id={id}) body: tag={tag}") if id is None: # create new tag - tag_id = db.create_tag_by_model(tag).tag_id + tag_id = db.create_tag_by_model(tag).id return {"id": tag_id} else: # update already existing tag - tag = Tag(tag_id=id, name=tag.name, description=tag.description, category_id=tag.category_id) + tag = Tag(id=id, name=tag.name, description=tag.description, category_id=tag.category_id) try: db.update_tag_by_model(tag) except ValueError as e: