How to update tables using SQL Alchemy ORM in python
Hi I am trying to update multiple tables with sql alchemy orm in python. I am new to sql alchemy orm. I wanted to make sure I am in the right direction how to properly query a table and update the tables using sql alchemy orm.
Here is what I am trying to accomplish according to this graph:
Here is my code
from cars.models import Cars
from cars.models import CarType
from cars.models import CarModel
from cars.models import CarItem, CarItemOther
from sqlalchemy import create_engine
from sqlalchemy.orm import session
from sqlalchemy.sql.expression import update
from sqlalchemy.sql.sqltypes import String, Text
# To update cars
def update_cars_models(session):
db = session
state = db.query(Cars).filter(Cars.state == "PRE_PROCESS").all()
car_update = ()
for row in state:
if (row.state == "PRE_PROCESS"):
car_update = CarType("Subaru", "WHITE", "PRE_PROCESS", Cars("Subaru", "PROCESS"))
# update and commit
update(car_update)
db.commit()
db.close()
return car_update
# To update each car models
def update_cars(car_update, session):
db = session
cars = car_update
cars_models = ()
for row in cars:
if (row.state == "PRE_PROCESS"):
cars_models = CarModel("PRE_PROCESS", CarType("Subaru", "PROCESSED"), CarItem("PROCESSED", CarItemOther("Payment Processed")))
# update and commit
update(cars_models)
db.commit()
car_process = db.query(CarType).filter(CarType.state == "PROCESSED").all()
cars_models_process = ()
for row in car_process:
if (row.state == "PROCESSED"):
cars_models_process = CarModel("PROCESSED", CarType("Subaru", "PROCESSED"), CarItem("PROCESSED", CarItemOther("Payment Processed")))
# update and commit
update(cars_models_process)
db.commit()
# close session
db.close()
return cars_models
if __name__ == "__main__":
# To update cars
car_up = update_cars_models(session)
update_cars(car_up, session)
from Recent Questions - Stack Overflow https://ift.tt/3rpAhoS
https://ift.tt/3D2mnew
Comments
Post a Comment