4.2 KiB
-
Select the hero
Spider-Boy
. -
Execute the select statement.
This generates the output:
INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age FROM hero WHERE hero.name = ? INFO Engine [no key 0.00018s] ('Spider-Boy',)
-
Get one hero object, the only one that should be there for Spider-Boy.
-
Print this hero.
This generates the output:
Hero 1: name='Spider-Boy' secret_name='Pedro Parqueador' age=None id=2
-
Select another hero.
-
Execute the select statement.
This generates the output:
INFO 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.00020s] ('Captain North America',)
!!! tip See the
BEGIN
at the top?This is SQLAlchemy automatically starting a transaction for us. This way, we could revert the last changes (if there were some) if we wanted to, even if the SQL to create them was already sent to the database.
-
Get one hero object for this new query.
The only one that should be there for Captain North America.
-
Print this second hero.
This generates the output:
Hero 2: name='Captain North America' secret_name='Esteban Rogelios' age=93 id=7
-
Update the age for the first hero.
Set the value of the attribute
age
to16
.This updates the hero object in memory, but not yet in the database.
-
Update the name of the first hero.
Now the name of the hero will not be
"Spider-Boy"
but"Spider-Youngster"
.Also, this updates the object in memory, but not yet in the database.
-
Add this first hero to the session.
This puts it in the temporary space in the session before committing it to the database.
It is not saved yet.
-
Update the name of the second hero.
Now the hero has a bit more precision in the name. 😜
This updates the object in memory, but not yet in the database.
-
Update the age of the second hero.
This updates the object in memory, but not yet in the database.
-
Add the second hero to the session.
This puts it in the temporary space in the session before committing it to the database.
-
Commit all the changes tracked in the session.
This commits everything in one single batch.
This generates the output:
INFO Engine UPDATE hero SET name=?, age=? WHERE hero.id = ? INFO Engine [generated in 0.00028s] (('Spider-Youngster', 16, 2), ('Captain North America Except Canada', 110, 7)) INFO Engine COMMIT
!!! tip See how SQLAlchemy (that powers SQLModel) optimizes the SQL to do as much work as possible in a single batch.
Here it updates both heroes in a single SQL query.
-
Refresh the first hero.
This generates the output:
INFO Engine BEGIN (implicit) INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age FROM hero WHERE hero.id = ? INFO Engine [generated in 0.00023s] (2,)
!!! tip Because we just committed a SQL transaction with
COMMIT
, SQLAlchemy will automatically start a new transaction withBEGIN
. -
Refresh the second hero.
This generates the output:
INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age FROM hero WHERE hero.id = ? INFO Engine [cached since 0.001709s ago] (7,)
!!! tip SQLAlchemy is still using the previous transaction, so it doesn't have to create a new one.
-
Print the first hero, now udpated.
This generates the output:
Updated hero 1: name='Spider-Youngster' secret_name='Pedro Parqueador' age=16 id=2
-
Print the second hero, now updated.
This generates the output:
Updated hero 2: name='Captain North America Except Canada' secret_name='Esteban Rogelios' age=110 id=7
-
Here is the end of the
with
block statement, so the session can execute its terminating code.The session will
ROLLBACK
(undo) any possible changes in the last transaction that were not committed.This generates the output:
INFO Engine ROLLBACK