Replies: 2 comments 2 replies
-
you most definitely don't want to have an optional field (xx | None) as a primary_key |
Beta Was this translation helpful? Give feedback.
2 replies
-
Facing the same issue, even with PK defined as NOT NULL. Edit: found that if you manually set the relationship field by doing a query before |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First Check
Commit to Help
Example Code
Description
Hello there,
i'm trying to create a relationship by using SQLModel and setting a UUID as foreign in the hardware model.
The field i'm talking about is the owner_id on the Hardware Model.
The value is sent as JSON to the endpoint and i've validated on multiple places in the code if owner_id is set. It is.
But every time i'm trying to write the data to the database i'm getting the following error:
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: hardware.owner_id
[SQL: INSERT INTO hardware (id, name, serial, image, category, owner_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)]
[parameters: ('0ac8aa2e6afa4d53a13c59064ac73090', 'test hardware', 'hdw-a0ed4b5b-f845-4a29-aa1b-e911313b11ca', None, 'CONTROLLER', None, '2024-07-21 20:56:07.016448', '2024-07-21 20:56:07.016448')]
I've followed the tutorial in the SQLModel Docs but I can't find the issue. Maybe someone sees something i'm overseeing but I don't know what else to do. As far as I can tell my model and my logic is correct.
I did some additional debugging and found, that in SQLAlchemy, in the persistence.py, there is a function called save_obj. Which is collecting the states_to_insert. When appending this array it's adding the state of the hardware, which at this point still has the owner_id. After adding it, it loses it. Maybe this helps, I don't know.
Thanks for your help in advance. If you need anymore information, feel free to ask.
Greetings from Germany
Operating System
macOS
Operating System Details
macOS Sonoma 14.3
SQLModel Version
0.0.19
Python Version
3.12.0
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions