diff --git a/h/migrations/versions/550865ed6622_update_mention_to_reference_annotation.py b/h/migrations/versions/550865ed6622_update_mention_to_reference_annotation.py new file mode 100644 index 00000000000..f4ceac4471e --- /dev/null +++ b/h/migrations/versions/550865ed6622_update_mention_to_reference_annotation.py @@ -0,0 +1,39 @@ +"""Update mention to reference annotation. + +Revision ID: 550865ed6622 +Revises: 39cc1025a3a2 +""" + +import sqlalchemy as sa +from alembic import op + +from h.db import types + +revision = "550865ed6622" +down_revision = "39cc1025a3a2" + + +def upgrade() -> None: + op.drop_column("mention", "annotation_id") + op.add_column( + "mention", + sa.Column( + "annotation_id", + types.URLSafeUUID(), + sa.ForeignKey("annotation.id", ondelete="CASCADE"), + nullable=False, + ), + ) + + +def downgrade() -> None: + op.drop_column("mention", "annotation_id") + op.add_column( + "mention", + sa.Column( + "annotation_id", + sa.INTEGER(), + sa.ForeignKey("annotation_slim.id", ondelete="CASCADE"), + nullable=False, + ), + ) diff --git a/h/models/annotation.py b/h/models/annotation.py index 7254a4630f4..f6611edd01e 100644 --- a/h/models/annotation.py +++ b/h/models/annotation.py @@ -138,6 +138,8 @@ class Annotation(Base): uselist=True, ) + mentions = sa.orm.relationship("Mention", back_populates="annotation") + @property def uuid(self): """ diff --git a/h/models/mention.py b/h/models/mention.py index 04afb6bfab6..be52ba700ad 100644 --- a/h/models/mention.py +++ b/h/models/mention.py @@ -1,7 +1,7 @@ import sqlalchemy as sa from sqlalchemy.orm import Mapped, mapped_column -from h.db import Base +from h.db import Base, types from h.db.mixins import Timestamps from h.models import helpers @@ -11,13 +11,13 @@ class Mention(Base, Timestamps): # pragma: nocover id: Mapped[int] = mapped_column(sa.Integer, autoincrement=True, primary_key=True) - annotation_id: Mapped[int] = mapped_column( - sa.Integer, - sa.ForeignKey("annotation_slim.id", ondelete="CASCADE"), + annotation_id: Mapped[types.URLSafeUUID] = mapped_column( + types.URLSafeUUID, + sa.ForeignKey("annotation.id", ondelete="CASCADE"), nullable=False, ) - """FK to annotation_slim.id""" - annotation = sa.orm.relationship("AnnotationSlim") + """FK to annotation.id""" + annotation = sa.orm.relationship("Annotation", back_populates="mentions") user_id: Mapped[int] = mapped_column( sa.Integer,