Insert twice with and without PK raises Duplicate primary key violates unique constraint #1267
Unanswered
meschac38700
asked this question in
Questions
Replies: 2 comments 12 replies
-
What if you use sqlalchemy? It's better to clarify which this issue is related to. |
Beta Was this translation helpful? Give feedback.
0 replies
-
@monchin Actually, I tried with sqlalchemy, no problem. the following code, using sqlalchemy's from sqlalchemy import insert
from sqlmodel import Session, SQLModel, create_engine
engine = create_engine("sqlite:///database.db")
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
# Using sqlalchemy's `insert` function
stmt = insert(Hero).values(id=1, name="username", secret_name="Full Username")
stmt2 = insert(Hero).values(name="username1", secret_name="Full Username2")
session.exec(stmt)
session.exec(stmt2) |
Beta Was this translation helpful? Give feedback.
12 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
Hello, I am encountering an error while using SQLModel/SQLAlchemy.
I am using PostrgreSQL database and when I try to insert a row, after specifying the id (primary key) attribute,
the second insert (without specifying the id) fails due to a conflict over the unique constraint on the primary key.
The second insert attempts to insert the row with id 1 which was already taken with the first insert in this case.
This seems weird to me because sometimes I need to save data with the ID (like for my tests, initial data etc.).
And not being able to specify an ID is quite annoying.
Is the problem known? because I didn't find any open issues regarding this.
Operating System
Linux
Operating System Details
OS: Manjaro Linux x86_64
Kernel: Linux 6.6.60-1-lts
SQLModel Version
0.0.22
Python Version
Python 3.13.0
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions