casacade delete tasks
This commit is contained in:
@@ -0,0 +1,65 @@
|
|||||||
|
"""add_cascade_delete_to_tasks
|
||||||
|
|
||||||
|
Revision ID: 197b195208e8
|
||||||
|
Revises: a2fdd0ec4a96
|
||||||
|
Create Date: 2025-08-06 23:41:56.778423
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = '197b195208e8'
|
||||||
|
down_revision: Union[str, None] = 'a2fdd0ec4a96'
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
"""Upgrade schema."""
|
||||||
|
op.execute("PRAGMA foreign_keys=ON")
|
||||||
|
|
||||||
|
with op.batch_alter_table('tasks', schema=None) as batch_op:
|
||||||
|
connection = op.get_bind()
|
||||||
|
inspector = sa.inspect(connection)
|
||||||
|
|
||||||
|
foreign_keys = inspector.get_foreign_keys('tasks')
|
||||||
|
constraint_name = None
|
||||||
|
|
||||||
|
for fk in foreign_keys:
|
||||||
|
if 'user_id' in fk['constrained_columns']:
|
||||||
|
constraint_name = fk['name']
|
||||||
|
break
|
||||||
|
|
||||||
|
if constraint_name:
|
||||||
|
try:
|
||||||
|
batch_op.drop_constraint(constraint_name, type_='foreignkey')
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
batch_op.create_foreign_key(
|
||||||
|
'fk_tasks_user_id_users',
|
||||||
|
'users',
|
||||||
|
['user_id'],
|
||||||
|
['id'],
|
||||||
|
ondelete='CASCADE'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
"""Downgrade schema."""
|
||||||
|
with op.batch_alter_table('tasks', schema=None) as batch_op:
|
||||||
|
try:
|
||||||
|
batch_op.drop_constraint('fk_tasks_user_id_users', type_='foreignkey')
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
batch_op.create_foreign_key(
|
||||||
|
'fk_tasks_user_id_users',
|
||||||
|
'users',
|
||||||
|
['user_id'],
|
||||||
|
['id']
|
||||||
|
)
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
"""fix_duplicate_foreign_keys
|
||||||
|
|
||||||
|
Revision ID: 4b0f3ea2fd26
|
||||||
|
Revises: 197b195208e8
|
||||||
|
Create Date: 2025-08-06 23:54:24.308488
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = '4b0f3ea2fd26'
|
||||||
|
down_revision: Union[str, None] = '197b195208e8'
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
"""Upgrade schema."""
|
||||||
|
op.execute("PRAGMA foreign_keys=ON")
|
||||||
|
|
||||||
|
with op.batch_alter_table('tasks', schema=None) as batch_op:
|
||||||
|
connection = op.get_bind()
|
||||||
|
inspector = sa.inspect(connection)
|
||||||
|
|
||||||
|
foreign_keys = inspector.get_foreign_keys('tasks')
|
||||||
|
|
||||||
|
for fk in foreign_keys:
|
||||||
|
if 'user_id' in fk['constrained_columns']:
|
||||||
|
try:
|
||||||
|
batch_op.drop_constraint(fk['name'], type_='foreignkey')
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
batch_op.create_foreign_key(
|
||||||
|
'fk_tasks_user_id_users',
|
||||||
|
'users',
|
||||||
|
['user_id'],
|
||||||
|
['id'],
|
||||||
|
ondelete='CASCADE'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
"""Downgrade schema."""
|
||||||
|
with op.batch_alter_table('tasks', schema=None) as batch_op:
|
||||||
|
try:
|
||||||
|
batch_op.drop_constraint('fk_tasks_user_id_users', type_='foreignkey')
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
batch_op.create_foreign_key(
|
||||||
|
'fk_tasks_user_id_users',
|
||||||
|
'users',
|
||||||
|
['user_id'],
|
||||||
|
['id']
|
||||||
|
)
|
||||||
@@ -16,7 +16,7 @@ priority_enum = Enum("low", "medium", "high", "critical", name="priority_enum")
|
|||||||
class TasksORM(Base):
|
class TasksORM(Base):
|
||||||
__tablename__ = "tasks"
|
__tablename__ = "tasks"
|
||||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||||
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
|
||||||
title: Mapped[str] = mapped_column(String(100))
|
title: Mapped[str] = mapped_column(String(100))
|
||||||
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||||
due_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
|
due_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
|
||||||
|
|||||||
@@ -23,4 +23,4 @@ class UsersORM(Base):
|
|||||||
avatar_path: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
avatar_path: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
||||||
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
|
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
|
||||||
is_superuser: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
is_superuser: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
||||||
tasks: Mapped[list["TasksORM"]] = relationship(back_populates="user")
|
tasks: Mapped[list["TasksORM"]] = relationship(back_populates="user", cascade="all, delete-orphan")
|
||||||
|
|||||||
Reference in New Issue
Block a user