tortoise / tortoise/tortoise-orm
Incorrect return value with tconn.execute_query('insert')
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
Describe the bug
Execute raw sql insert, result is incorrect.
To Reproduce
from tortoise import Tortoise, fields, run_async
from tortoise.models import Model
class Event(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
class Meta:
table = "event"
def __str__(self):
return self.name
async def run():
# await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
await Tortoise.init(db_url="postgres://user:pass@127.0.0.1:5432/example", modules={"models": ["__main__"]})
await Tortoise.generate_schemas()
from tortoise.transactions import in_transaction
try:
async with in_transaction() as tconn:
r = await tconn.execute_query("insert into event values (10, '2')")
print(r)
print(await tconn.execute_query('select * from event'))
except Exception as e:
print(e)
await tconn.rollback()
if __name__ == "__main__":
run_async(run())
# You got this:
# (0, [])
# (6, [<Record id=1 name='2'>])
# Sqlite got this:
(1, [])
(1, [<sqlite3.Row object at 0x00000208DDA86ED0>])
Expected behavior
(1, [])
Additional context
It would be better to return lastrowid
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The entry point is the transaction connection's execute_query, with the reproduction comparing PostgreSQL and SQLite INSERT results; start there and compare both backend paths. Add a regression test for an INSERT and verify that the returned result matches the expected row-count and last-row information for each backend.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, python, sqlite
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100