2.4 KiB
-
Select the hero we will delete.
-
Execute the query with the select statement object.
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.00011s] ('Spider-Youngster',)
-
Get one hero object, expecting exactly one.
!!! tip This ensures there's no more than one, and that there's exactly one, not
None
.This would never return `None`, instead it would raise an exception.
-
Print the hero object.
This generates the output:
Hero: name='Spider-Youngster' secret_name='Pedro Parqueador' age=16 id=2
-
Delete the hero from the session.
This marks the hero as deleted from the session, but it will not be removed from the database until we commit the changes.
-
Commit the session.
This saves the changes in the session, including deleting this row.
It generates the output:
INFO Engine DELETE FROM hero WHERE hero.id = ? INFO Engine [generated in 0.00020s] (2,) INFO Engine COMMIT
-
Print the deleted hero object.
The hero is deleted in the database. And is marked as deleted in the session.
But we still have the object in memory with its data, so we can use it to print it.
This generates the output:
Deleted hero: name='Spider-Youngster' secret_name='Pedro Parqueador' age=16 id=2
-
Select the same hero again.
We'll do this to confirm if the hero is really deleted.
-
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.00013s] ('Spider-Youngster',)
-
Get the "first" item from the
results
.If no items were found, this will return
None
, which is what we expect. -
Check if the first item from the results is
None
. -
If this first item is indeed
None
, it means that it was correctly deleted from the database.Now we can print a message to confirm.
This generates the output:
There's no hero named Spider-Youngster
-
This is the end of the
with
block, here the session executes its closing code.This generates the output:
INFO Engine ROLLBACK