From 87ddb544400e8cf26a765853e666c00805dc6496 Mon Sep 17 00:00:00 2001 From: seeleng Date: Mon, 23 Sep 2024 02:58:11 +0800 Subject: [PATCH] fix: add event to notes --- .../d79f0e188c13_add_events_to_note.py | 44 +++++++++++++++++++ backend/src/notes/models.py | 11 ++++- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 backend/alembic/versions/d79f0e188c13_add_events_to_note.py diff --git a/backend/alembic/versions/d79f0e188c13_add_events_to_note.py b/backend/alembic/versions/d79f0e188c13_add_events_to_note.py new file mode 100644 index 00000000..38ee0dd1 --- /dev/null +++ b/backend/alembic/versions/d79f0e188c13_add_events_to_note.py @@ -0,0 +1,44 @@ +"""Add events to note + +Revision ID: d79f0e188c13 +Revises: 37cf2356fe08 +Create Date: 2024-09-23 02:56:34.941075 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "d79f0e188c13" +down_revision: Union[str, None] = "37cf2356fe08" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "event_note", + sa.Column("event_id", sa.Integer(), nullable=False), + sa.Column("note_id", sa.Integer(), nullable=False), + sa.ForeignKeyConstraint( + ["event_id"], + ["event.id"], + ), + sa.ForeignKeyConstraint( + ["note_id"], + ["note.id"], + ), + sa.PrimaryKeyConstraint("event_id", "note_id"), + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table("event_note") + # ### end Alembic commands ### diff --git a/backend/src/notes/models.py b/backend/src/notes/models.py index 9b27cf94..8b8cada8 100644 --- a/backend/src/notes/models.py +++ b/backend/src/notes/models.py @@ -1,5 +1,5 @@ from sqlalchemy import Enum, ForeignKey -from sqlalchemy.orm import Mapped, mapped_column +from sqlalchemy.orm import Mapped, mapped_column, relationship from src.common.base import Base @@ -24,3 +24,12 @@ class Note(Base): user_id: Mapped[int] = mapped_column(ForeignKey("user.id")) __mapper_args__ = {"polymorphic_on": "parent_type", "polymorphic_identity": "note"} + + events = relationship("Event", secondary="event_note") + + +class EventNote(Base): + __tablename__ = "event_note" + + event_id: Mapped[int] = mapped_column(ForeignKey("event.id"), primary_key=True) + note_id: Mapped[int] = mapped_column(ForeignKey("note.id"), primary_key=True)