tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.39k stars 359 forks source link

Big bug on create || post > id increment on fail post wasting unique id and spamming db #1495

Closed xalteropsx closed 8 months ago

xalteropsx commented 8 months ago

275183896-3cbf1fb2-d7a8-4a3f-a2bf-921c4475ef92

id keep keep increase even on fail post while this issue not occur on sqlalchmey

YAGregor commented 8 months ago

it's not bug, you may need learn more about database, mysql never promise auto increase field continuous, why sqlachemy is not the same? might because that sqlalchemy check data first and it didn't execute create sql statement..

xalteropsx commented 8 months ago
  data = _mod(**_request.model_dump(), user_id=_user.id)
   database.add(data)
    database.flush()

not sure i use sort of this technique to avoid waste of id incremental value

xalteropsx commented 8 months ago

it's not bug, you may need learn more about database, mysql never promise auto increase field continuous, why sqlachemy is not the same? might because that sqlalchemy check data first and it didn't execute create sql statement..

is their anything alternative to session.flush() ? in tortoise neither their is an session.add()

xalteropsx commented 8 months ago

@YAGregor u need to check the flush method that is the key of not incrementing the id in sqlachemy while here we have nothing about flush i think tortoise not added anything related to flush or docs

xalteropsx commented 8 months ago

image image

xalteropsx commented 8 months ago

image even ponyorm has flush ...

xalteropsx commented 8 months ago

@grigi why tortoise not has flush which is the most powerful method ?

feature: 1.it avoid fail attempt id to not trigger incremental 2.it allow to multi post table cause it get the id before post

my 70% code is base on flush and yet this orm not have i see all other orm already supported flush

xalteropsx commented 8 months ago

    db_module = mod(**module.dict())
    database.add(db_module)
    database.flush()

    db_group = mod1(**group.dict(), module_id=db_module.id)
    database.add(db_group)

    database.commit()
    database.refresh(db_module)
    database.refresh(db_group)

    return {
        "module": db_module,
        "group": db_group,
    }
YAGregor commented 8 months ago

the flush is apart of sqlalchemy's "unit of work", tortoise orm would never has flush, cause tortoise, just like django, follow "active record pattern", it's very different from sqlalchemy (from idea level), for your case, you just to need to avoid insert fail, or use another data source(like redis) to make autoincreasing sequence

xalteropsx commented 8 months ago

nvm friend i left tortoise yestarday i saw rust orm which build for fastapi i love it almost 12k star in 7month of its release

YAGregor commented 8 months ago

nvm friend i left tortoise yestarday i saw rust orm which build for fastapi i love it almost 12k star in 7month of its release

what rust orm? I never heard

xalteropsx commented 8 months ago

@YAGregor Edgedb