Upsert
In [1]:
Copied!
from sqlmodel import Session, select
from chowda.models import SonyCiAsset
from chowda.db import engine
from sqlmodel import Session, select
from chowda.models import SonyCiAsset
from chowda.db import engine
In [2]:
Copied!
def get_asset():
with Session(engine) as session:
z = session.exec(select(SonyCiAsset).where(SonyCiAsset.id == 'test'))
x = z.first()
def get_asset():
with Session(engine) as session:
z = session.exec(select(SonyCiAsset).where(SonyCiAsset.id == 'test'))
x = z.first()
In [3]:
Copied!
from sqlalchemy.dialects.postgresql import insert
c = SonyCiAsset(
id='test',
name='testname',
type='Video',
size=1,
format='mp4',
thumbnails=[],
)
c.model_dump()
from sqlalchemy.dialects.postgresql import insert
c = SonyCiAsset(
id='test',
name='testname',
type='Video',
size=1,
format='mp4',
thumbnails=[],
)
c.model_dump()
Out[3]:
{'name': 'testname', 'size': 1, 'type': <MediaType.video: 'Video'>, 'format': 'mp4', 'thumbnails': []}
In [4]:
Copied!
def upsert(asset):
with Session(engine) as session:
stmt = (
insert(SonyCiAsset)
.values([asset])
.on_conflict_do_update(
index_elements=[SonyCiAsset.id],
set_=asset,
)
)
session.execute(stmt)
session.commit()
def upsert(asset):
with Session(engine) as session:
stmt = (
insert(SonyCiAsset)
.values([asset])
.on_conflict_do_update(
index_elements=[SonyCiAsset.id],
set_=asset,
)
)
session.execute(stmt)
session.commit()