Adds relationships and CASCADE on deletes
This commit is contained in:
parent
246c948d76
commit
91514f71b1
1
.gitignore
vendored
1
.gitignore
vendored
@ -153,4 +153,5 @@ dmypy.json
|
||||
|
||||
### Default user directories
|
||||
export/
|
||||
tmp/
|
||||
.pfbudget
|
||||
|
||||
109
alembic/versions/287fe9e6682a_add_relationships.py
Normal file
109
alembic/versions/287fe9e6682a_add_relationships.py
Normal file
@ -0,0 +1,109 @@
|
||||
"""Add relationships
|
||||
|
||||
Revision ID: 287fe9e6682a
|
||||
Revises: d3534f493239
|
||||
Create Date: 2022-12-03 16:43:39.633382+00:00
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "287fe9e6682a"
|
||||
down_revision = "d3534f493239"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_constraint(
|
||||
"fk_categorized_id_originals",
|
||||
"categorized",
|
||||
schema="transactions",
|
||||
type_="foreignkey",
|
||||
)
|
||||
op.create_foreign_key(
|
||||
op.f("fk_categorized_id_originals"),
|
||||
"categorized",
|
||||
"originals",
|
||||
["id"],
|
||||
["id"],
|
||||
source_schema="transactions",
|
||||
referent_schema="transactions",
|
||||
ondelete="CASCADE",
|
||||
)
|
||||
op.drop_constraint(
|
||||
"fk_notes_id_originals", "notes", schema="transactions", type_="foreignkey"
|
||||
)
|
||||
op.create_foreign_key(
|
||||
op.f("fk_notes_id_originals"),
|
||||
"notes",
|
||||
"originals",
|
||||
["id"],
|
||||
["id"],
|
||||
source_schema="transactions",
|
||||
referent_schema="transactions",
|
||||
ondelete="CASCADE",
|
||||
)
|
||||
op.drop_constraint(
|
||||
"fk_tags_id_originals", "tags", schema="transactions", type_="foreignkey"
|
||||
)
|
||||
op.create_foreign_key(
|
||||
op.f("fk_tags_id_originals"),
|
||||
"tags",
|
||||
"originals",
|
||||
["id"],
|
||||
["id"],
|
||||
source_schema="transactions",
|
||||
referent_schema="transactions",
|
||||
ondelete="CASCADE",
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_constraint(
|
||||
op.f("fk_tags_id_originals"), "tags", schema="transactions", type_="foreignkey"
|
||||
)
|
||||
op.create_foreign_key(
|
||||
"fk_tags_id_originals",
|
||||
"tags",
|
||||
"originals",
|
||||
["id"],
|
||||
["id"],
|
||||
source_schema="transactions",
|
||||
referent_schema="transactions",
|
||||
)
|
||||
op.drop_constraint(
|
||||
op.f("fk_notes_id_originals"),
|
||||
"notes",
|
||||
schema="transactions",
|
||||
type_="foreignkey",
|
||||
)
|
||||
op.create_foreign_key(
|
||||
"fk_notes_id_originals",
|
||||
"notes",
|
||||
"originals",
|
||||
["id"],
|
||||
["id"],
|
||||
source_schema="transactions",
|
||||
referent_schema="transactions",
|
||||
)
|
||||
op.drop_constraint(
|
||||
op.f("fk_categorized_id_originals"),
|
||||
"categorized",
|
||||
schema="transactions",
|
||||
type_="foreignkey",
|
||||
)
|
||||
op.create_foreign_key(
|
||||
"fk_categorized_id_originals",
|
||||
"categorized",
|
||||
"originals",
|
||||
["id"],
|
||||
["id"],
|
||||
source_schema="transactions",
|
||||
referent_schema="transactions",
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
@ -1,3 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import (
|
||||
BigInteger,
|
||||
Enum,
|
||||
@ -7,7 +9,13 @@ from sqlalchemy import (
|
||||
String,
|
||||
Text,
|
||||
)
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, MappedAsDataclass
|
||||
from sqlalchemy.orm import (
|
||||
DeclarativeBase,
|
||||
Mapped,
|
||||
mapped_column,
|
||||
MappedAsDataclass,
|
||||
relationship,
|
||||
)
|
||||
|
||||
from decimal import Decimal
|
||||
from typing import Annotated, Optional
|
||||
@ -66,16 +74,26 @@ class Original(Base):
|
||||
bank: Mapped[bankfk]
|
||||
amount: Mapped[money]
|
||||
|
||||
|
||||
idfk = Annotated[int, mapped_column(BigInteger, ForeignKey(Original.id))]
|
||||
category: Mapped[Category] = relationship(back_populates="original")
|
||||
note: Mapped[Note] = relationship(back_populates="original")
|
||||
tags: Mapped[set[Tag]] = relationship(
|
||||
back_populates="original", cascade="all, delete-orphan", passive_deletes=True
|
||||
)
|
||||
|
||||
|
||||
class Categorized(Base):
|
||||
idfk = Annotated[
|
||||
int, mapped_column(BigInteger, ForeignKey(Original.id, ondelete="CASCADE"))
|
||||
]
|
||||
|
||||
|
||||
class Category(Base):
|
||||
__tablename__ = "categorized"
|
||||
|
||||
id: Mapped[idfk] = mapped_column(primary_key=True)
|
||||
category: Mapped[str]
|
||||
|
||||
original: Mapped[Original] = relationship(back_populates="category")
|
||||
|
||||
|
||||
class Note(Base):
|
||||
__tablename__ = "notes"
|
||||
@ -83,6 +101,8 @@ class Note(Base):
|
||||
id: Mapped[idfk] = mapped_column(primary_key=True)
|
||||
note: Mapped[str]
|
||||
|
||||
original: Mapped[Original] = relationship(back_populates="note")
|
||||
|
||||
|
||||
class Nordigen(Base):
|
||||
__tablename__ = "nordigen"
|
||||
@ -93,8 +113,10 @@ class Nordigen(Base):
|
||||
invert: Mapped[Optional[bool]]
|
||||
|
||||
|
||||
class Tags(Base):
|
||||
class Tag(Base):
|
||||
__tablename__ = "tags"
|
||||
|
||||
id: Mapped[idfk] = mapped_column(primary_key=True)
|
||||
tag: Mapped[str] = mapped_column(primary_key=True)
|
||||
|
||||
original: Mapped[Original] = relationship(back_populates="tags")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user