feat: add ListingDecision model and Alembic migration
Add ListingDecision SQLModel entity for tracking user like/dislike decisions on properties, with unique constraint on (user_id, listing_id, listing_type) and appropriate indexes.
This commit is contained in:
parent
e7eb8523cb
commit
3d2a24e921
2 changed files with 64 additions and 0 deletions
51
alembic/versions/d6e7f8a9b0c1_add_listing_decision_table.py
Normal file
51
alembic/versions/d6e7f8a9b0c1_add_listing_decision_table.py
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
"""add listing decision table
|
||||||
|
|
||||||
|
Revision ID: d6e7f8a9b0c1
|
||||||
|
Revises: c5d6e7f8a9b0
|
||||||
|
Create Date: 2026-02-21 12:00:00.000000
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
import sqlmodel
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = 'd6e7f8a9b0c1'
|
||||||
|
down_revision: Union[str, None] = 'c5d6e7f8a9b0'
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
"""Create listingdecision table."""
|
||||||
|
op.create_table('listingdecision',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('listing_id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('listing_type', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
|
sa.Column('decision', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
|
sa.Column('created_at', sa.DateTime(), nullable=True,
|
||||||
|
server_default=sa.func.now()),
|
||||||
|
sa.Column('updated_at', sa.DateTime(), nullable=True,
|
||||||
|
server_default=sa.func.now()),
|
||||||
|
sa.ForeignKeyConstraint(['user_id'], ['user.id']),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('user_id', 'listing_id', 'listing_type',
|
||||||
|
name='uq_decision_user_listing'),
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_listingdecision_user_id'),
|
||||||
|
'listingdecision', ['user_id'], unique=False)
|
||||||
|
op.create_index(op.f('ix_listingdecision_listing_id'),
|
||||||
|
'listingdecision', ['listing_id'], unique=False)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
"""Drop listingdecision table."""
|
||||||
|
op.drop_index(op.f('ix_listingdecision_listing_id'),
|
||||||
|
table_name='listingdecision')
|
||||||
|
op.drop_index(op.f('ix_listingdecision_user_id'),
|
||||||
|
table_name='listingdecision')
|
||||||
|
op.drop_table('listingdecision')
|
||||||
13
models/decision.py
Normal file
13
models/decision.py
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from sqlmodel import SQLModel, Field
|
||||||
|
|
||||||
|
|
||||||
|
class ListingDecision(SQLModel, table=True):
|
||||||
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
|
user_id: int = Field(nullable=False, foreign_key="user.id", index=True)
|
||||||
|
listing_id: int = Field(nullable=False, index=True)
|
||||||
|
listing_type: str = Field(nullable=False) # "RENT" or "BUY"
|
||||||
|
decision: str = Field(nullable=False) # "liked" or "disliked"
|
||||||
|
created_at: datetime = Field(default_factory=datetime.utcnow)
|
||||||
|
updated_at: datetime = Field(default_factory=datetime.utcnow)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue