Skip to content

Commit

Permalink
Create relationship between Price & Product
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Nov 21, 2023
1 parent 09721a7 commit 5fa739a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 50 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Add Price.product relationship
Revision ID: 7103fb49908f
Revises: df15f7cacf48
Create Date: 2023-11-21 20:13:21.521496
"""
from typing import Sequence, Union

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "7103fb49908f"
down_revision: Union[str, None] = "df15f7cacf48"
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.add_column("prices", sa.Column("product_id", sa.Integer(), nullable=True))
op.create_foreign_key(None, "prices", "products", ["product_id"], ["id"])
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, "prices", type_="foreignkey")
op.drop_column("prices", "product_id")
# ### end Alembic commands ###
4 changes: 4 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class Product(Base):
off_quantity = Column(Integer)
off_image_url = Column(String)

prices: Mapped[list["Price"]] = relationship(back_populates="product")

created = Column(DateTime(timezone=True), server_default=func.now())
updated = Column(DateTime(timezone=True), onupdate=func.now())

Expand Down Expand Up @@ -87,6 +89,8 @@ class Price(Base):
id = Column(Integer, primary_key=True, index=True)

product_code = Column(String, index=True)
product_id: Mapped[int] = mapped_column(ForeignKey("products.id"), nullable=True)
product: Mapped[Product] = relationship(back_populates="prices")

price = Column(Numeric(precision=10, scale=2))
currency = Column(CurrencyType)
Expand Down

0 comments on commit 5fa739a

Please sign in to comment.