Skip to content
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

Use columns query parameter for insert #175

Closed
steve-chavez opened this issue Apr 29, 2021 · 1 comment · Fixed by #176
Closed

Use columns query parameter for insert #175

steve-chavez opened this issue Apr 29, 2021 · 1 comment · Fixed by #176
Labels
enhancement New feature or request released

Comments

@steve-chavez
Copy link
Member

Feature request

With the columns query parameter, the All object keys must match error message is avoided. Instead the direct PostgreSQL error is shown, which is more helpful for debugging.

Example:

// When making a bulk insert
const { data, error } = await supabase
  .from('mytable')
  .insert([
    {
      field: 'val2',
      nonNullableField: 'val3'        
    },
    {
      field: 'val1'     
    }      
])

// Right now this happens
console.log(error);
{message: 'All object keys must match'}

// If we were to use the columns query param, we'd get a clearer error
console.log(error);
{
    "code": "23502",
    "details": "Failing row contains (val1, null).",
    "hint": null,
    "message": "null value in column \"nonNullableField\" violates not-null constraint"
}

Note: This doesn't solve #173 because the default value of the columns won't be applied. It would still clear the error message though.

Possible implementation

Is not necessary to add a new method to the lib. Internally, the body of the bulk insert can be passed through a function that gets the keys and produces the columns query param.

Something like:

let body = [
    {
      field: 'val2',
      nonNullableField: 'val3'        
    },
    {
      field: 'val1',
      anotherField: 'val4'     
    }      
];

// Using https://ramdajs.com/
let columns = R.join(',', R.uniq(R.flatten(R.map(R.keys, body))));

console.log(columns);
"field,nonNullableField,anotherField"
// append this to ?columns= in the http request
@github-actions
Copy link

github-actions bot commented May 9, 2021

🎉 This issue has been resolved in version 0.28.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant