From 7fc025821b703f7faeef5c11bde5c4e329ed0ab7 Mon Sep 17 00:00:00 2001 From: Javanaut Date: Thu, 26 Sep 2024 17:50:48 +0200 Subject: [PATCH] Fix delete cascade --- bin/ffx/ffx_app.py | 13 ++++++++++++- bin/ffx/model/pattern.py | 2 +- bin/ffx/model/show.py | 11 +++++++++++ bin/ffx/show_delete_screen.py | 8 +++++++- bin/ffx/shows_screen.py | 2 +- 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/bin/ffx/ffx_app.py b/bin/ffx/ffx_app.py index 771a6c6..75ca93a 100644 --- a/bin/ffx/ffx_app.py +++ b/bin/ffx/ffx_app.py @@ -1,4 +1,4 @@ -import os +import os, time, sqlite3, sqlalchemy from textual.app import App, ComposeResult from textual.screen import Screen @@ -53,6 +53,17 @@ class FfxApp(App): 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: diff --git a/bin/ffx/model/pattern.py b/bin/ffx/model/pattern.py index 9f8838f..fe8d550 100644 --- a/bin/ffx/model/pattern.py +++ b/bin/ffx/model/pattern.py @@ -1,5 +1,5 @@ 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 diff --git a/bin/ffx/model/show.py b/bin/ffx/model/show.py index e1889db..c367923 100644 --- a/bin/ffx/model/show.py +++ b/bin/ffx/model/show.py @@ -5,11 +5,20 @@ from sqlalchemy.orm import relationship, declarative_base, sessionmaker Base = declarative_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' # v1.x id = Column(Integer, primary_key=True) + name = Column(String) year = Column(Integer) @@ -19,7 +28,9 @@ class Show(Base): # year: Mapped[int] = mapped_column(Integer, nullable=False) # 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") # v2.0 # patterns: Mapped[List["Pattern"]] = relationship(back_populates="show", cascade="all, delete") diff --git a/bin/ffx/show_delete_screen.py b/bin/ffx/show_delete_screen.py index c4aacee..a6f7aba 100644 --- a/bin/ffx/show_delete_screen.py +++ b/bin/ffx/show_delete_screen.py @@ -110,8 +110,14 @@ class ShowDeleteScreen(Screen): s = self.Session() q = s.query(Show).filter(Show.id == int(show_id)) + if q.count(): - q.delete() + + #DAFUQ: https://stackoverflow.com/a/19245058 + # q.delete() + show = q.first() + s.delete(show) + s.commit() return True return False diff --git a/bin/ffx/shows_screen.py b/bin/ffx/shows_screen.py index 47d52f7..a0052c1 100644 --- a/bin/ffx/shows_screen.py +++ b/bin/ffx/shows_screen.py @@ -125,7 +125,7 @@ class ShowsScreen(Screen): return [(int(s.id), s.name, s.year) for s in q.all()] except Exception as ex: - click.ClickException(f"ShowsScreen(): {repr(ex)}") + click.ClickException(f"loadShows(): {repr(ex)}") finally: s.close()