"UniqueViolation, duplicate key value violates unique constraint" on update #581
-
First Check
Commit to Help
Example Codefrom pgvector.sqlalchemy import Vector
from sqlalchemy import text
## model
class Record(SQLModel, table=True):
id: UUID = Field(default_factory=uuid4, primary_key=True)
text: str = Field(default=None)
start: int = Field(default=None)
vector: List[float] = Field(default=None, sa_column=Vector(1536))
parent_id: UUID = Field(default=None, foreign_key="parent.id")
## controller
def update_record(session: Session, id: UUID, record: RecordUpdate):
query = text("SELECT * FROM record WHERE id = :id")
result = session.execute(query, {"id": id}).fetchone()
if not result:
return None
db_record = Record.from_orm(result)
if not db_record:
return None
for key, value in record.dict().items():
if hasattr(db_record, key) and value is not None:
setattr(db_record, key, value)
session.add(db_record)
session.commit()
session.refresh(db_record)
return db_record DescriptionI have an edpoint for updating records in the database, but it doesn't seem to work as intended and I get the following error when I call the endpoint to update a record:
In another controller I'm using Operating SystemmacOS Operating System DetailsNo response SQLModel Version0.0.8 Python Version3.9.13 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
I'm having the exact same problem right now. For me, it's only happening when I change things in the model's relationship. This is definitely buggy behavior, as this should not cause the model to be seen as new. stmt = select(MyModel).where(MyModel.indexed_column == query_value)
existing = self.db.execute(stmt).scalar_one_or_none()
existing.relationship_to_things = [thing1, thing2, thing3]
self.db.commit() gives unique key constraint violation on the primary key of I've tried calling |
Beta Was this translation helpful? Give feedback.
-
See above, at least for me, this happens when I try to replace or add to a relationship for my existing row. To get around this, I did the following: for hero in existing_team.heroes:
session.delete(hero)
session.flush()
for hero in new_heroes:
db_hero = Hero(
name = hero.name,
# etc.
)
session.add(db_hero)
session.commit() |
Beta Was this translation helpful? Give feedback.
-
I guest you should not use |
Beta Was this translation helpful? Give feedback.
-
I stumbled upon this thread and I have solved a similar issue with merge(). I'm not sure if this is really the same problem but I guess it doesn't hurt to mention it here. Note that merge() comes with its own drawbacks as written below under "Merge Tips". basically replace add() with merge(). |
Beta Was this translation helpful? Give feedback.
I stumbled upon this thread and I have solved a similar issue with merge(). I'm not sure if this is really the same problem but I guess it doesn't hurt to mention it here. Note that merge() comes with its own drawbacks as written below under "Merge Tips".
basically replace add() with merge().
See: https://docs.sqlalchemy.org/en/20/orm/session_state_management.html#merging