-
-
Notifications
You must be signed in to change notification settings - Fork 143
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
Error 'All object keys must match' #173
Comments
Hello @peachp, We can treat this as a bug, but it must be said that in SQL this expression is also not valid. INSERT INTO TABLE_NAME (column_a,
column_b)
VALUES ('aaa'),
('aaa', 'bbb');
|
@ftonato yep, I know that the equivalent SQL wouldn't work, but that's kind of is the point of Supabase for me: an API wrapper around Postgres, which simplifies the hosting, design, as well as querying the DB. I don't see any disadvantages to default missing / mismatching keys to If you still think there is some negative side effect or tradeoff, maybe you could at least make the error message more descriptive. |
I agree with you! It would be good if it worked, maybe it would make things easier in some scenarios (like perhaps the current one you are facing). On the other hand, I wonder the operation cost to check if the missing fields accept empty values, if you have defined default values to use or if they have not, then should use Anyway, I think you are right to open the discussion as a whole, but my comment above was more for treating as a "bug", something that is not described in the documentation. |
Unfortunately setting it to null will try to explicitly insert a Perhaps If that doesn't work we'd have to map over all the values, then run the inserts in batches of matching keys - but this is a bit of a fringe case so I know if it's worth the added complexity (perf loss) for our library |
@ftonato I was not sure if it is a bug, so feel free to change it to enhancement / feature request.
Is it bad to to explicitly insert a NULL value? I don't know the details of NULL vs Default vs "empty" in Postgres. For example with the ERP system I worked with, there were never NULL values in the DB - it made sure to write some default values like empty string or 0.
It was a one-off migration task I already finished. However, my understanding is that
then So currently I understand I have only these options then:
Or am I missing something? But again, I'm happy to be educated why it is good to always make the developer decide each time if it should be NULL or some default value. |
I would like to know about this myself since I'm trying to upsert. In my situation I can have references to the ids of the rows but in the same operation I also need to add new rows of which I do not have an identifier. This returns summing up the upsert does not do what I expect that is to add new rows and update those already in db in the same operation. this forces me to have to make two separate api calls for insert and update |
A workaround for making null identifiers work is to create a trigger that converts the null to the default id. Like: -- Change items and its sequence according to your table
CREATE OR REPLACE FUNCTION items_null_id_is_default() RETURNS TRIGGER AS $$
BEGIN
NEW.id = coalesce(NEW.id, nextval('items_id_seq'));
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER items_null_id_is_default
BEFORE INSERT ON items FOR EACH ROW EXECUTE PROCEDURE items_null_id_is_default(); Then you can avoid the [
{
id: null,
name: 'a name'
},
{
id: 123,
name: 'another name'
}
] ( Once PostgREST/postgrest#1567 is cleared out then the library will allow this out-of-the-box. |
Do I have to create the sequence ('items_id_seq') manually or is it automatically created by supabase? |
I found the answer here: https://stackoverflow.com/a/10332015. |
+1 for allowing this pattern |
It feels like it should at least allow the upsert with missmatching keys when a default value is provided for the missmatched keys in the table. So that upsert like this is possible: |
+1 for this. I mean, I thought that is what upsert is for. I have multiple rows...if I add a new one, it doesn't have an ID yet...so, I would expect existing items get updated, while the new one, gets inserted. |
+1 for this as well. trigger only feels like a workaround and not expected behavior. |
any update for this? been quite for a while now. |
..bumped into this error, you guys saved my day. |
FYI, this is being worked at PostgREST/postgrest#2672. |
Bug report
Describe the bug
When trying to insert an array of objects, where in some objects the key
field2
is present, and in others isn't, the JS API client errors to: 'All object keys must match'. Thefield2
is not mandatory.Desired behavior
The JS client / Supabase should default the mismatching fields to
null
or whatever is translated to NULL in Postgres. (As workaround I set the fields tonull
and it looks OK in my case.)The text was updated successfully, but these errors were encountered: