From e3d0014e62e2b24871299b77c417cec3160a426a Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Mon, 15 Nov 2021 16:34:58 +0100 Subject: [PATCH] [pricelist] Use Serial database type instead of int for IDs --- flaschengeist/plugins/pricelist/models.py | 43 ++++++++++++----------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/flaschengeist/plugins/pricelist/models.py b/flaschengeist/plugins/pricelist/models.py index be36247..48298dc 100644 --- a/flaschengeist/plugins/pricelist/models.py +++ b/flaschengeist/plugins/pricelist/models.py @@ -1,20 +1,21 @@ from __future__ import annotations # TODO: Remove if python requirement is >= 3.10 from flaschengeist.database import db +from flaschengeist.models import ModelSerializeMixin, Serial from flaschengeist.models.image import Image from typing import Optional drink_tag_association = db.Table( "drink_x_tag", - db.Column("drink_id", db.Integer, db.ForeignKey("drink.id")), - db.Column("tag_id", db.Integer, db.ForeignKey("drink_tag.id")), + db.Column("drink_id", Serial, db.ForeignKey("drink.id")), + db.Column("tag_id", Serial, db.ForeignKey("drink_tag.id")), ) drink_type_association = db.Table( "drink_x_type", - db.Column("drink_id", db.Integer, db.ForeignKey("drink.id")), - db.Column("type_id", db.Integer, db.ForeignKey("drink_type.id")), + db.Column("drink_id", Serial, db.ForeignKey("drink.id")), + db.Column("type_id", Serial, db.ForeignKey("drink_type.id")), ) @@ -24,7 +25,7 @@ class Tag(db.Model, ModelSerializeMixin): """ __tablename__ = "drink_tag" - id: int = db.Column("id", db.Integer, primary_key=True) + id: int = db.Column("id", Serial, primary_key=True) name: str = db.Column(db.String(30), nullable=False, unique=True) color: str = db.Column(db.String(7), nullable=False) @@ -35,7 +36,7 @@ class DrinkType(db.Model, ModelSerializeMixin): """ __tablename__ = "drink_type" - id: int = db.Column("id", db.Integer, primary_key=True) + id: int = db.Column("id", Serial, primary_key=True) name: str = db.Column(db.String(30), nullable=False, unique=True) @@ -45,9 +46,9 @@ class DrinkPrice(db.Model, ModelSerializeMixin): """ __tablename__ = "drink_price" - id: int = db.Column("id", db.Integer, primary_key=True) + id: int = db.Column("id", Serial, primary_key=True) price: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False)) - volume_id_ = db.Column("volume_id", db.Integer, db.ForeignKey("drink_price_volume.id")) + volume_id_ = db.Column("volume_id", Serial, db.ForeignKey("drink_price_volume.id")) volume: "DrinkPriceVolume" = None _volume: "DrinkPriceVolume" = db.relationship("DrinkPriceVolume", back_populates="_prices", join_depth=1) public: bool = db.Column(db.Boolean, default=True) @@ -62,8 +63,8 @@ class ExtraIngredient(db.Model, ModelSerializeMixin): ExtraIngredient """ - __tablename__ = "extra_ingredient" - id: int = db.Column("id", db.Integer, primary_key=True) + __tablename__ = "drink_extra_ingredient" + id: int = db.Column("id", Serial, primary_key=True) name: str = db.Column(db.String(30), unique=True, nullable=False) price: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False)) @@ -74,9 +75,9 @@ class DrinkIngredient(db.Model, ModelSerializeMixin): """ __tablename__ = "drink_ingredient" - id: int = db.Column("id", db.Integer, primary_key=True) + id: int = db.Column("id", Serial, primary_key=True) volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False), nullable=False) - ingredient_id: int = db.Column(db.Integer, db.ForeignKey("drink.id")) + ingredient_id: int = db.Column(Serial, db.ForeignKey("drink.id")) cost_per_volume: float name: str _drink_ingredient: Drink = db.relationship("Drink") @@ -95,14 +96,14 @@ class Ingredient(db.Model, ModelSerializeMixin): Ingredient Associationtable """ - __tablename__ = "ingredient_association" - id: int = db.Column("id", db.Integer, primary_key=True) - volume_id = db.Column(db.Integer, db.ForeignKey("drink_price_volume.id")) + __tablename__ = "drink_ingredient_association" + id: int = db.Column("id", Serial, primary_key=True) + volume_id = db.Column(Serial, db.ForeignKey("drink_price_volume.id")) drink_ingredient: Optional[DrinkIngredient] = db.relationship(DrinkIngredient) extra_ingredient: Optional[ExtraIngredient] = db.relationship(ExtraIngredient) - _drink_ingredient_id = db.Column(db.Integer, db.ForeignKey("drink_ingredient.id")) - _extra_ingredient_id = db.Column(db.Integer, db.ForeignKey("extra_ingredient.id")) + _drink_ingredient_id = db.Column(Serial, db.ForeignKey("drink_ingredient.id")) + _extra_ingredient_id = db.Column(Serial, db.ForeignKey("drink_extra_ingredient.id")) class MinPrices(ModelSerializeMixin): @@ -120,8 +121,8 @@ class DrinkPriceVolume(db.Model, ModelSerializeMixin): """ __tablename__ = "drink_price_volume" - id: int = db.Column("id", db.Integer, primary_key=True) - drink_id = db.Column(db.Integer, db.ForeignKey("drink.id")) + id: int = db.Column("id", Serial, primary_key=True) + drink_id = db.Column(Serial, db.ForeignKey("drink.id")) drink: "Drink" = None _drink: "Drink" = db.relationship("Drink", back_populates="_volumes") volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False)) @@ -143,7 +144,7 @@ class Drink(db.Model, ModelSerializeMixin): """ __tablename__ = "drink" - id: int = db.Column("id", db.Integer, primary_key=True) + id: int = db.Column("id", Serial, primary_key=True) article_id: Optional[str] = db.Column(db.String(64)) package_size: Optional[int] = db.Column(db.Integer) name: str = db.Column(db.String(60), nullable=False) @@ -152,9 +153,9 @@ class Drink(db.Model, ModelSerializeMixin): cost_per_package: Optional[float] = db.Column(db.Numeric(precision=5, scale=3, asdecimal=False)) has_image: bool = False - uuid: str = db.Column(db.String(36)) receipt: Optional[list[str]] = db.Column(db.PickleType(protocol=4)) + _type_id = db.Column("type_id", Serial, db.ForeignKey("drink_type.id")) _image_id = db.Column("image_id", Serial, db.ForeignKey("image.id")) image_: Image = db.relationship("Image", cascade="all, delete", foreign_keys=[_image_id])