23 lines
645 B
Python
23 lines
645 B
Python
from collections.abc import Sequence
|
|
from typing import Any
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
def inserted_primary_key(result: Any) -> int:
|
|
lastrowid = getattr(result, "lastrowid", None)
|
|
if lastrowid is not None:
|
|
return int(lastrowid)
|
|
|
|
try:
|
|
inserted_primary_key = result.inserted_primary_key
|
|
except Exception:
|
|
inserted_primary_key = None
|
|
|
|
if isinstance(inserted_primary_key, Sequence) and inserted_primary_key:
|
|
primary_key = inserted_primary_key[0]
|
|
if primary_key is not None:
|
|
return int(primary_key)
|
|
|
|
raise RuntimeError("Could not determine inserted primary key")
|