Skip to content

Commit 81dd1a4

Browse files
committed
Refactored order_product_association_table into an SQLAlchemy model; added a 'count' column.
1 parent f87b95e commit 81dd1a4

11 files changed

Lines changed: 31 additions & 21 deletions

alembic/versions/2025_07_02_1623-8980aba26f80_qwerty.py renamed to alembic/versions/2025_08_18_1451-00afda171644_add_count_column_to_order_product_.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
"""qwerty
1+
"""add count column to order product association table
22
3-
Revision ID: 8980aba26f80
3+
Revision ID: 00afda171644
44
Revises: 76e5a8b4d1bc
5-
Create Date: 2025-07-02 16:23:43.528638
5+
Create Date: 2025-08-18 14:51:22.005793
66
77
"""
88

@@ -13,7 +13,7 @@
1313

1414

1515
# revision identifiers, used by Alembic.
16-
revision: str = "8980aba26f80"
16+
revision: str = "00afda171644"
1717
down_revision: Union[str, Sequence[str], None] = "76e5a8b4d1bc"
1818
branch_labels: Union[str, Sequence[str], None] = None
1919
depends_on: Union[str, Sequence[str], None] = None
@@ -22,12 +22,15 @@
2222
def upgrade() -> None:
2323
"""Upgrade schema."""
2424
# ### commands auto generated by Alembic - please adjust! ###
25-
pass
25+
op.add_column(
26+
"order_product_association",
27+
sa.Column("count", sa.Integer(), server_default="1", nullable=False),
28+
)
2629
# ### end Alembic commands ###
2730

2831

2932
def downgrade() -> None:
3033
"""Downgrade schema."""
3134
# ### commands auto generated by Alembic - please adjust! ###
32-
pass
35+
op.drop_column("order_product_association", "count")
3336
# ### end Alembic commands ###
-170 Bytes
Binary file not shown.
-2.08 KB
Binary file not shown.
-929 Bytes
Binary file not shown.
-1.33 KB
Binary file not shown.
-2.29 KB
Binary file not shown.

‎core/models/__init__.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"Post",
88
"Profile",
99
"Order",
10-
"order_product_assosiation_table"
10+
"OrderProductAssociation",
1111
)
1212

1313

@@ -18,4 +18,4 @@
1818
from .post import Post
1919
from .profile import Profile
2020
from .order import Order
21-
from .order_product_association import order_product_assosiation_table
21+
from .order_product_association import OrderProductAssociation

‎core/models/order.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from sqlalchemy.orm import Mapped, mapped_column, relationship
77

88
from .base import Base
9-
from .order_product_association import order_product_assosiation_table
109

1110

1211
if TYPE_CHECKING:
@@ -23,7 +22,7 @@ class Order(Base):
2322
default=datetime.utcnow,
2423
)
2524
products: Mapped[list["Product"]] = relationship(
26-
secondary=order_product_assosiation_table,
25+
secondary="order_product_association",
2726
back_populates="orders",
2827
# lazy="noload"
2928
)
Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
from sqlalchemy import Column, Integer, ForeignKey, Table, UniqueConstraint
1+
from sqlalchemy import ForeignKey, UniqueConstraint
2+
from sqlalchemy.orm import Mapped, mapped_column
23

34
from .base import Base
45

56

6-
order_product_assosiation_table = Table(
7-
"order_product_association",
8-
Base.metadata,
9-
Column("id", Integer, primary_key=True),
10-
Column("order_id", ForeignKey("orders.id"), nullable=False),
11-
Column("product_id", ForeignKey("products.id"), nullable=False),
12-
UniqueConstraint("order_id", "product_id", name="idx_unique_order_product")
13-
)
7+
class OrderProductAssociation(Base):
8+
9+
__tablename__ = "order_product_association"
10+
__table_args__ = (
11+
UniqueConstraint(
12+
"order_id",
13+
"product_id",
14+
name="idx_unique_order_product",
15+
),
16+
)
17+
18+
id: Mapped[int] = mapped_column(primary_key=True)
19+
order_id: Mapped[int] = mapped_column(ForeignKey("orders.id"))
20+
product_id: Mapped[int] = mapped_column(ForeignKey("products.id"))
21+
count: Mapped[int] = mapped_column(default=1, server_default="1")
22+
# unit_price: Mapped[int] = mapped_column(default=1)

‎core/models/product.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from sqlalchemy.orm import Mapped, relationship
44

55
from .base import Base
6-
from .order_product_association import order_product_assosiation_table
76

87
if TYPE_CHECKING:
98
from .order import Order
@@ -16,6 +15,6 @@ class Product(Base):
1615
description: Mapped[str]
1716
price: Mapped[int]
1817
orders: Mapped[list["Order"]] = relationship(
19-
secondary=order_product_assosiation_table,
18+
secondary="order_product_association",
2019
back_populates="products",
2120
)

0 commit comments

Comments
 (0)