Skip to content
Discussion options

You must be logged in to vote

session.add() is used to add new object (which is not present in the DB).
If you need to update existing object - use session.merge():

        hero_copy = session.merge(hero_copy)

Important thing is that not hero_copy will be linked to the session, but it's copy. This is why we need to store the result of session.merge() into variable to be able to refresh it later.

Runnable code example in the details:

import pytest

from sqlmodel import Field, Session, SQLModel, create_engine, select


sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"

engine = create_engine(sqlite_url, echo=True)


class Hero(SQLModel, table=True):
    id: int = Field(default=None, nullable=F…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
2 participants