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.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:
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user