How to create two tables at the same time in FAST API

Viewed 24

i want to create two tables at the same time. and also use one tables primary key to another tables foreign key at the same time.

def createOrderPlace(request: OrderCreatePlaceOrder, db: Session):
        #try:

        
            x = format(random.randint(10000,99999), '04d')
            exists_x=db.query(OrderModel).filter(OrderModel.order_id == x).first()
            if exists_x:
                x = format(random.randint(10000,99999), '04d') 
     
            order_create = OrderModel(
                user_id=request.currentUser.user_id,
                name=request.currentUser.name,
                email=request.currentUser.email,
                orderAmount=request.subtotal,
                cvv_id=request.currentUser.cvv_id,
                card_name=request.currentUser.card_name,
                card_number=request.currentUser.card_number,
                card_month=request.currentUser.card_month,
                card_year=request.currentUser.card_year,
                order_id=x,

                billing_first_name=request.currentUser.billing_first_name,
                billing_last_name=request.currentUser.billing_last_name,
                billing_phone_number=request.currentUser.billing_phone_number,
                billing_country=request.currentUser.billing_country,
                billing_division=request.currentUser.billing_division,
                billing_district=request.currentUser.billing_district,
                billing_address=request.currentUser.billing_address,
                billing_police_station=request.currentUser.billing_police_station,
                billing_post_code=request.currentUser.billing_post_code,

                shipping_first_name=request.currentUser.shipping_first_name,
                shipping_last_name=request.currentUser.shipping_last_name,
                shipping_phone_number=request.currentUser.shipping_phone_number,
                shipping_country=request.currentUser.shipping_country,
                shipping_division=request.currentUser.shipping_division,
                shipping_district=request.currentUser.shipping_district,
                shipping_address=request.currentUser.shipping_address,
                shipping_police_station=request.currentUser.shipping_police_station,
                shipping_post_code=request.currentUser.shipping_post_code,

                


            )

            db.add(order_create)

            db_orderid = (
                db.query(OrderModel)
                .filter(OrderModel.user_id == request.currentUser.user_id)
                .first()
            )

           

            for dic in request.cartItems:
                order_item_a = OrderItemsModel(
                    item_id=dic.item_id,
                    name=dic.item_name,
                    price=dic.item_price,
                    description=dic.item_description,
                    user_id=dic.user_id,
                    payment_order_id=dic.payment_order_id,
                )

                db.add(order_item_a)

            db.commit()
        

            
            return {'invoice':request,'detail': f'Successfully Buy this Service, Check it on My service','message':True,'order_number':order_create.order_id}

i tried this but thats not work. I am using two models

class OrderModel(Base): **(model 1)**
    __tablename__ = "tbl_order_and_payment-order"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String)
    email = Column(String)
    orderAmount = Column(Integer)
    transactionId = Column(String)
    isDelivered = Column(Boolean)
    user_id = Column(Integer, ForeignKey('tbl_stu_usr-users.id'))
    purchase_date = Column(DateTime, default=datetime.utcnow)
    #updated_at = Column(DateTime, default=datetime.utcnow)
    cvv_id=Column(String)
    card_name=Column(String)
    card_number=Column(String)
    card_month=Column(Integer)
    card_year=Column(Integer)
    order_id = Column(Integer)

    billing_first_name=Column(String)
    billing_last_name=Column(String)
    billing_phone_number=Column(String)
    billing_country=Column(String)
    billing_division=Column(String)
    billing_district=Column(String)
    billing_address=Column(String)
    billing_police_station=Column(String)
    billing_post_code=Column(Integer)

i have another model

class OrderItemsModel(Base):
    __tablename__ = "tbl_order_and_payment-order_items"

    id = Column(Integer, primary_key=True, index=True)
    item_id=Column(Integer, ForeignKey('tbl_stu_adm-pricing_plan-service.id'))
    user_id = Column(Integer, ForeignKey('tbl_stu_usr-users.id'))
    name = Column(String)
    price = Column(Integer)
    description=Column(String)
    product_type=Column(String)
    payment_order_id=Column(Integer, ForeignKey('tbl_order_and_payment-order.id'))

i am using "tbl_order_and_payment-order.id" this primary tho this "tbl_order_and_payment-order_items" tables as payment_order_id=Column(Integer, ForeignKey('tbl_order_and_payment-order.id')) i just want to create two tables at the same time.

0 Answers
Related