Fix delete cascade
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import os
|
import os, time, sqlite3, sqlalchemy
|
||||||
|
|
||||||
from textual.app import App, ComposeResult
|
from textual.app import App, ComposeResult
|
||||||
from textual.screen import Screen
|
from textual.screen import Screen
|
||||||
@@ -53,6 +53,17 @@ class FfxApp(App):
|
|||||||
|
|
||||||
Base.metadata.create_all(self.context['database_engine'])
|
Base.metadata.create_all(self.context['database_engine'])
|
||||||
|
|
||||||
|
# isSyncronuous = False
|
||||||
|
# while not isSyncronuous:
|
||||||
|
# while True:
|
||||||
|
# try:
|
||||||
|
# with self.context['database_engine'].connect() as connection:
|
||||||
|
# connection.execute(sqlalchemy.text('PRAGMA foreign_keys=ON;'))
|
||||||
|
# #isSyncronuous = True
|
||||||
|
# break
|
||||||
|
# except sqlite3.OperationalError:
|
||||||
|
# time.sleep(0.1)
|
||||||
|
|
||||||
|
|
||||||
def on_mount(self) -> None:
|
def on_mount(self) -> None:
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
|
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
|
||||||
from sqlalchemy.orm import relationship, sessionmaker, Mapped
|
from sqlalchemy.orm import relationship, sessionmaker, Mapped, backref
|
||||||
|
|
||||||
from .show import Base
|
from .show import Base
|
||||||
|
|
||||||
|
|||||||
@@ -5,11 +5,20 @@ from sqlalchemy.orm import relationship, declarative_base, sessionmaker
|
|||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
|
||||||
class Show(Base):
|
class Show(Base):
|
||||||
|
"""
|
||||||
|
relationship(argument, opt1, opt2, ...)
|
||||||
|
argument is string of class or Mapped class of the target entity
|
||||||
|
backref creates a bi-directional corresponding relationship (back_populates preferred)
|
||||||
|
back_populates points to the corresponding relationship (the actual class attribute identifier)
|
||||||
|
|
||||||
|
See: https://docs.sqlalchemy.org/en/(14|20)/orm/basic_relationships.html
|
||||||
|
"""
|
||||||
|
|
||||||
__tablename__ = 'shows'
|
__tablename__ = 'shows'
|
||||||
|
|
||||||
# v1.x
|
# v1.x
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
|
|
||||||
name = Column(String)
|
name = Column(String)
|
||||||
year = Column(Integer)
|
year = Column(Integer)
|
||||||
|
|
||||||
@@ -19,7 +28,9 @@ class Show(Base):
|
|||||||
# year: Mapped[int] = mapped_column(Integer, nullable=False)
|
# year: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||||
|
|
||||||
# v1.x
|
# v1.x
|
||||||
|
#patterns = relationship('Pattern', back_populates='show', cascade="all, delete", passive_deletes=True)
|
||||||
patterns = relationship('Pattern', back_populates='show', cascade="all, delete")
|
patterns = relationship('Pattern', back_populates='show', cascade="all, delete")
|
||||||
|
# patterns = relationship('Pattern', back_populates='show', cascade="all")
|
||||||
|
|
||||||
# v2.0
|
# v2.0
|
||||||
# patterns: Mapped[List["Pattern"]] = relationship(back_populates="show", cascade="all, delete")
|
# patterns: Mapped[List["Pattern"]] = relationship(back_populates="show", cascade="all, delete")
|
||||||
|
|||||||
@@ -110,8 +110,14 @@ class ShowDeleteScreen(Screen):
|
|||||||
s = self.Session()
|
s = self.Session()
|
||||||
q = s.query(Show).filter(Show.id == int(show_id))
|
q = s.query(Show).filter(Show.id == int(show_id))
|
||||||
|
|
||||||
|
|
||||||
if q.count():
|
if q.count():
|
||||||
q.delete()
|
|
||||||
|
#DAFUQ: https://stackoverflow.com/a/19245058
|
||||||
|
# q.delete()
|
||||||
|
show = q.first()
|
||||||
|
s.delete(show)
|
||||||
|
|
||||||
s.commit()
|
s.commit()
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ class ShowsScreen(Screen):
|
|||||||
return [(int(s.id), s.name, s.year) for s in q.all()]
|
return [(int(s.id), s.name, s.year) for s in q.all()]
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
click.ClickException(f"ShowsScreen(): {repr(ex)}")
|
click.ClickException(f"loadShows(): {repr(ex)}")
|
||||||
finally:
|
finally:
|
||||||
s.close()
|
s.close()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user