def vehicle_register(db: Session, vehicle: update_vehicle): """Creates a new vehicle registration from data sent via API""" new_vehicle = vehicle(**vehicle.dict()) db.add(new_vehicle) db.commit() db.reflesh(new_vehicle) return new_vehicle
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String from sqlalchemy import create_engine from settings import DB_CONNECTION_STRING from sqlalchemy import Session Base = declarative_base() class Person(Base): __tablename__ = "Peaople" id = Column(Integer, primery_key=True) name = Column(String(20)) engine = create_engine(DB_CONNECTION_STRING) Base.metadata.create_all(engine) session = Session(bind=engine) kolio = Person(name="Kolio") session.add(kolio) seesion.commit()
Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) # A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() # Insert a Person in the person table new_person1 = Person(name='Keith') session.add(new_person1) # this adds the person to the session new_person2 = Person(name='Joe') session.add(new_person1) new_person3 = Person(name='Steve') session.add(new_person1) session.commit() # commit saves the changes # Insert an Address in the address table using a loop addresses = [ Address(post_code='00001', person=new_person1), Address(post_code='00002', person=new_person2), Address(post_code='00003', person=new_person3), ]