DELETE is very similar, and again we use FROM to tell the table to work on, and we use WHERE to tell the condition to use to match the rows that we want to delete.
You can try that in DB Browser for SQLite:
Have in mind that DELETE is to delete entire rows, not single values in a row.
If you want to "delete" a single value in a column while keeping the row, you would instead update the row as explained in the previous chapter, setting the specific value of the column in that row to NULL (to None in Python).
Now let's delete with SQLModel.
To get the same results, delete the database.db file before running the examples.
fromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:int|None=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:int|None=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
fromtypingimportOptionalfromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:Optional[int]=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:Optional[int]=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
As this is a new function delete_heroes(), we'll also add it to the main() function so that we call it when executing the program from the command line:
fromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:int|None=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:int|None=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
fromtypingimportOptionalfromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:Optional[int]=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:Optional[int]=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
That will print the same existing hero Spider-Youngster:
fast βpython app.py π¬ Some boilerplate and previous output omitted π π¬ The SELECT with WHEREINFO Engine BEGIN (implicit) INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age FROM hero WHERE hero.name = ? INFO Engine [no key 0.00011s] ('Spider-Youngster',)
π¬ Print the hero as obtained from the databaseHero: name='Spider-Youngster' secret_name='Pedro Parqueador' age=16 id=2
fromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:int|None=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:int|None=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
fromtypingimportOptionalfromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:Optional[int]=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:Optional[int]=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
fromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:int|None=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:int|None=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
fromtypingimportOptionalfromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:Optional[int]=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:Optional[int]=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
The same as we have seen before, .commit() will also save anything else that was added to the session. Including updates, or created heroes.
This commit after deleting the hero will generate this output:
fast βpython app.py π¬ Some boilerplate output omitted π π¬ Previous output omitted π π¬ The SQL to update the hero in the databaseINFO Engine DELETE FROM hero WHERE hero.id = ? INFO Engine [generated in 0.00020s] (2,) INFO Engine COMMIT
fromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:int|None=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:int|None=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
fromtypingimportOptionalfromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:Optional[int]=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:Optional[int]=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
This will output:
fast βpython app.py π¬ Some boilerplate output omitted π π¬ Previous output omitted π π¬ Print the deleted heroDeleted hero: name='Spider-Youngster' secret_name='Pedro Parqueador' age=16 id=2
fromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:int|None=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:int|None=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
fromtypingimportOptionalfromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:Optional[int]=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:Optional[int]=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
Here we are using results.first() to get the first object found (in case it found multiple) or None, if it didn't find anything.
If we used results.one() instead, it would raise an exception, because it expects exactly one result.
And because we just deleted that hero, this should not find anything and we should get None.
This will execute some SQL in the database and output:
fast βpython app.py π¬ Some boilerplate output omitted π π¬ Previous output omitted π π¬ Automatically start a new transactionINFO Engine BEGIN (implicit)
π¬ SQL to search for the heroINFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age FROM hero WHERE hero.name = ? INFO Engine [no key 0.00013s] ('Spider-Youngster',)
Now let's just confirm that, indeed, no hero was found in the database with that name.
We'll do it by checking that the "first" item in the results is None:
# Code above omitted πdefdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")# Code below omitted π
# Code above omitted πdefdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")# Code below omitted π
π Full file preview
fromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:int|None=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:int|None=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
fromtypingimportOptionalfromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:Optional[int]=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:Optional[int]=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
This will output:
fast βpython app.py π¬ Some boilerplate output omitted π π¬ Previous output omitted π π¬ Indeed, the hero was deleted π₯There's no hero named Spider-Youngster
π¬ Cleanup after the with blockINFO Engine ROLLBACK
fromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:int|None=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:int|None=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
fromtypingimportOptionalfromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:Optional[int]=Field(default=None,primary_key=True)name:str=Field(index=True)secret_name:strage:Optional[int]=Field(default=None,index=True)sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url,echo=True)defcreate_db_and_tables():SQLModel.metadata.create_all(engine)defcreate_heroes():hero_1=Hero(name="Deadpond",secret_name="Dive Wilson")hero_2=Hero(name="Spider-Boy",secret_name="Pedro Parqueador")hero_3=Hero(name="Rusty-Man",secret_name="Tommy Sharp",age=48)hero_4=Hero(name="Tarantula",secret_name="Natalia Roman-on",age=32)hero_5=Hero(name="Black Lion",secret_name="Trevor Challa",age=35)hero_6=Hero(name="Dr. Weird",secret_name="Steve Weird",age=36)hero_7=Hero(name="Captain North America",secret_name="Esteban Rogelios",age=93)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.add(hero_4)session.add(hero_5)session.add(hero_6)session.add(hero_7)session.commit()defupdate_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")results=session.exec(statement)hero_1=results.one()print("Hero 1:",hero_1)statement=select(Hero).where(Hero.name=="Captain North America")results=session.exec(statement)hero_2=results.one()print("Hero 2:",hero_2)hero_1.age=16hero_1.name="Spider-Youngster"session.add(hero_1)hero_2.name="Captain North America Except Canada"hero_2.age=110session.add(hero_2)session.commit()session.refresh(hero_1)session.refresh(hero_2)print("Updated hero 1:",hero_1)print("Updated hero 2:",hero_2)defdelete_heroes():withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.one()print("Hero: ",hero)session.delete(hero)session.commit()print("Deleted hero:",hero)statement=select(Hero).where(Hero.name=="Spider-Youngster")results=session.exec(statement)hero=results.first()ifheroisNone:print("There's no hero named Spider-Youngster")defmain():create_db_and_tables()create_heroes()update_heroes()delete_heroes()if__name__=="__main__":main()
Tip
Check out the number bubbles to see what is done by each line of code.
To delete rows with SQLModel you just have to .delete() them with the session, and then, as always, .commit() the session to save the changes to the database. π₯