-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
One to One relationship causing Not-Null Constraint Violation on Nested Data Insert #2576
Comments
It's also good note that when you change the relationship to be a "one-to-many" making it such that "A" hasMany "B" by removing the unique constraint on the foreign key constraint, the error disappears |
i'm getting this error too |
I had issues similar to this. After doing some research, setting DEFERRABLE INITIALLY DEFERRED on my Foreign Key definition (added this manually to my initial migration script) fixed the issue. |
can you pliz give an example script |
same error for me. Any solutions for one-to-one relations? |
@howientc do you have some more infos how you did this? |
The server currently does not distinguish object relationships into 'many-to-one' and 'one-to-one' relationships. It currently treats all object relationships as 'many-to-one' because of which the ordering of inserts when there are one-to-one relationships is incorrect. This should not be hard to fix. When defining an object relationship using manual configuration we can add an extra field which indicates the {
"type": "create_object_relationship",
"args": {
"table": "author",
"name": "setting",
"type": "one_to_one",
"using": {
"manual_configuration": {
"remote_table": "author_setting",
"column_mapping": {
"id": "author_id"
}
}
}
}
} The What would be great though is to add support at the server to determine 'one-to-one' relationships automatically which can be done after this. |
After further discussion with @rikinsk we finalised on having {
"type": "create_object_relationship",
"args": {
"table": "author",
"name": "setting",
"type": "one_to_one",
"using": {
"manual_configuration": {
"remote_table": "author_setting",
"column_mapping": {
"id": "author_id"
},
"insertion_order": "after_parent"
}
}
}
}
|
@0x777 for now all I do is to make sure to call the mutation based on where the foreign key constraint is saved. But you are saying that with a next update this will be possible from both sides? Is there any roadmap to track process? |
Yes. You can follow this issue. We will share a link to a PR build when it is ready. |
@0x777 Is this issue still being worked on? Do you have an estimate when this could be fixed? Thanks! |
I get this error in the latest version. Is there any information on when to fix it? |
@marionschleifer @0x777 @emahuni @beepsoft I think I found the cause of the error. In the same request, I see the following logs where I send "x-hasura-admin-secret" and "Authorization" together. and I get an error. I only see the following logs when I send "Authorization" value in the same request. and the query runs successfully. I think a lot of people are experiencing it. It may be useful to indicate this in the documentation or to make an improvement. thanks for this beautiful tool :) |
I have taken this approach as well. I was getting: I removed the one to one relationship and manually added a one to many (Array relationship) with the UI, afterwards I was getting a fkey constraint error instead: I have removed the Foreign Key constraint that I added with the UI and manually added one with DEFERRABLE INITIALLY DEFERRED. The fkey links the two elements and Behavior of fkeys is as expected but now nested data inserts work fine even if they are inserted in a specific order as mentioned by documentation. Here is the SQL I used to manually add the fkey form the Hasura "SQL" option on the bottom left of the the Data panel: This now works for me: |
@0x777 Is this still an issue you are working on ? Can we help in some way ? This is not very handy in lots of situations |
I just stumbled across this issue during the implementation of my API into my Android App. I have a bills table (table A) and a few subtables (table B) and only the subtables reference to the bills table. And I have the case where I want to insert through table A but hit this exact issue. This is a quick mockup of my case:
So a Bill can have both a Type 1 and Type 2, but only ONE of each. Thus I cannot make any many-to-(any) relation, at least I dont see where to place it. I dont want to make the generic bill table reference any Type X subtable as I will get more subtables over time and migrating a huge table in production is not to my liking. Is there any ETA on a fix for this issue? Currently my only option to overcome the issue is to first insert into generic bill and after that insert into each subtable manually using the UUID of the bill. This by no means is my preferred fix as it causes more requests and code to do the same thing, if this where fixed. |
Is this still worked on? We are currently designing a new data base model for our cloud platform, and would like to use hasura, but this issue would be a bit of a set back, for our model. |
Fix is done and should land in master soon. |
as someone has been using hasura for roughly ~2 years in production, this is definitely a candidate for least helpful error messages. super frustrating as a lot of things in hasura just work. |
@eviefp Is there a PR or commit I can track to see when this actually lands? I'm on 2-beta2 and still hitting this. |
I changed, |
How can I set |
.yaml file of the table (metadata). Default is |
@rikinsk New relationship UI is still missing a insertion order dropdown. |
For anyone still struggling with this, I was able to get this to work in the UI by exporting the Metadata json file (gear in the top right hand corner of hasura UI), changing the relationships to |
The following table definitions reproduce the above error:
Tables
Table a:
Table B:
Relationships
Table A:
Table B:
Invoking The Error
That error happens when inserting data from the "has one" side (table a), but not when inserting from the "belongs to" side (I wish Hasura could label relationships like this as well in addition to what's there to make things clear quickly):
the above error happens when you run this query:
but not when you run this query from the other side:
The text was updated successfully, but these errors were encountered: