@ -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?