diff --git a/backend/alembic/versions/0002_social_graph.py b/backend/alembic/versions/0002_social_graph.py index 4c5d543..c7c5c9c 100644 --- a/backend/alembic/versions/0002_social_graph.py +++ b/backend/alembic/versions/0002_social_graph.py @@ -23,7 +23,7 @@ def upgrade() -> None: sa.Column("id", sa.Integer(), nullable=False), sa.Column("requester_id", sa.Integer(), nullable=False), sa.Column("addressee_id", sa.Integer(), nullable=False), - sa.Column("status", sa.String(length=20), nullable=False), + sa.Column("status", sa.String(length=20), server_default="pending", nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True), sa.CheckConstraint( @@ -48,7 +48,7 @@ def upgrade() -> None: sa.UniqueConstraint( "requester_id", "addressee_id", - name="uq_friendships_pair", + name="uq_friendships_requester_addressee", ), ) op.create_index(op.f("ix_friendships_id"), "friendships", ["id"], unique=False) @@ -57,6 +57,7 @@ def upgrade() -> None: op.create_table( "follows", + sa.Column("id", sa.Integer(), nullable=False), sa.Column("follower_id", sa.Integer(), nullable=False), sa.Column("followed_id", sa.Integer(), nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), @@ -74,17 +75,20 @@ def upgrade() -> None: ["users.id"], ondelete="CASCADE", ), - sa.PrimaryKeyConstraint("follower_id", "followed_id"), + sa.PrimaryKeyConstraint("id"), sa.UniqueConstraint( "follower_id", "followed_id", - name="uq_follows_pair", + name="uq_follows_follower_followed", ), ) + op.create_index(op.f("ix_follows_id"), "follows", ["id"], unique=False) op.create_index(op.f("ix_follows_followed_id"), "follows", ["followed_id"], unique=False) + op.create_index(op.f("ix_follows_follower_id"), "follows", ["follower_id"], unique=False) op.create_table( "blocks", + sa.Column("id", sa.Integer(), nullable=False), sa.Column("blocker_id", sa.Integer(), nullable=False), sa.Column("blocked_id", sa.Integer(), nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), @@ -102,22 +106,28 @@ def upgrade() -> None: ["users.id"], ondelete="CASCADE", ), - sa.PrimaryKeyConstraint("blocker_id", "blocked_id"), + sa.PrimaryKeyConstraint("id"), sa.UniqueConstraint( "blocker_id", "blocked_id", - name="uq_blocks_pair", + name="uq_blocks_blocker_blocked", ), ) + op.create_index(op.f("ix_blocks_id"), "blocks", ["id"], unique=False) op.create_index(op.f("ix_blocks_blocked_id"), "blocks", ["blocked_id"], unique=False) + op.create_index(op.f("ix_blocks_blocker_id"), "blocks", ["blocker_id"], unique=False) def downgrade() -> None: """Drop friendships, follows, and blocks tables.""" + op.drop_index(op.f("ix_blocks_blocker_id"), table_name="blocks") op.drop_index(op.f("ix_blocks_blocked_id"), table_name="blocks") + op.drop_index(op.f("ix_blocks_id"), table_name="blocks") op.drop_table("blocks") + op.drop_index(op.f("ix_follows_follower_id"), table_name="follows") op.drop_index(op.f("ix_follows_followed_id"), table_name="follows") + op.drop_index(op.f("ix_follows_id"), table_name="follows") op.drop_table("follows") op.drop_index(op.f("ix_friendships_requester_id"), table_name="friendships") diff --git a/backend/app/models/block.py b/backend/app/models/block.py index f92a54c..d2db387 100644 --- a/backend/app/models/block.py +++ b/backend/app/models/block.py @@ -13,7 +13,7 @@ class Block(Base): UniqueConstraint( "blocker_id", "blocked_id", - name="uq_blocks_blocker_blocked", + name="uq_blocks_pair", ), CheckConstraint( "blocker_id <> blocked_id", @@ -39,7 +39,7 @@ class Block(Base): blocker = relationship( "User", foreign_keys=[blocker_id], - back_populates="blocks_given", + back_populates="blocks_issued", ) blocked = relationship( "User", diff --git a/backend/app/models/follow.py b/backend/app/models/follow.py index f0af635..a050a36 100644 --- a/backend/app/models/follow.py +++ b/backend/app/models/follow.py @@ -13,7 +13,7 @@ class Follow(Base): UniqueConstraint( "follower_id", "followed_id", - name="uq_follows_follower_followed", + name="uq_follows_pair", ), CheckConstraint( "follower_id <> followed_id", @@ -39,7 +39,7 @@ class Follow(Base): follower = relationship( "User", foreign_keys=[follower_id], - back_populates="follows_given", + back_populates="following", ) followed = relationship( "User", diff --git a/backend/app/models/friendship.py b/backend/app/models/friendship.py index c8f72fc..ea75e78 100644 --- a/backend/app/models/friendship.py +++ b/backend/app/models/friendship.py @@ -28,7 +28,7 @@ class Friendship(Base): UniqueConstraint( "requester_id", "addressee_id", - name="uq_friendships_requester_addressee", + name="uq_friendships_pair", ), CheckConstraint( "requester_id <> addressee_id", @@ -65,10 +65,10 @@ class Friendship(Base): requester = relationship( "User", foreign_keys=[requester_id], - back_populates="friendships_sent", + back_populates="sent_friend_requests", ) addressee = relationship( "User", foreign_keys=[addressee_id], - back_populates="friendships_received", + back_populates="received_friend_requests", )