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

Support different fields in insert #1319

Open
2 tasks done
Magnuti opened this issue Dec 3, 2024 · 1 comment
Open
2 tasks done

Support different fields in insert #1319

Magnuti opened this issue Dec 3, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Magnuti
Copy link

Magnuti commented Dec 3, 2024

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

Having a table that looks like this

CREATE TABLE IF NOT EXISTS "public"."items" (
    "id" "uuid" DEFAULT "gen_random_uuid"() NOT NULL,
    "user_id" "uuid" NOT NULL,
);

When inserting new items in bulk with different fields (id only present on one item)

await supabaseClient.from("items").insert([
    {
        id: randomUUID(),
        user_id: userId,
    },
    {
        user_id: userId,
    },
])

Both items should be inserted; one with the provided id and one with a random UUID as id.

However I get this error

{
  code: "23502",
  details: "Failing row contains (null, e40a9b26-2a32-429f-ae71-9fada8ed4a33),
  hint: null,
  message: 'null value in column "id" of relation "items" violates not-null constraint'
}

It seems that gen_random_uuid is ignored.

To Reproduce

Follow the steps above.

Expected behavior

Both items should be inserted; one with the provided id and one with a random UUID as id.

Additional context

Both this

await supabaseClient.from("items").insert([
    {
        user_id: userId,
    },
    {
        user_id: userId,
    },
])

and this works

await supabaseClient.from("items").insert([
    {
        id: randomUUID(),
        user_id: userId,
    },
    {
        id: randomUUID(),
        user_id: userId,
    },
])

The solution here is to split the insert operation into two bulk inserts; one where id is present on all items and another where id is absent on all items. But this seems like a hack.

@Magnuti Magnuti added the bug Something isn't working label Dec 3, 2024
@Magnuti
Copy link
Author

Magnuti commented Dec 3, 2024

This seems to be handled well in the Swift library after a similar bug was found there supabase/supabase-swift#203 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant