edgedb icon indicating copy to clipboard operation
edgedb copied to clipboard

Allow ELSE after UNLESS CONFLICT

Open quinchs opened this issue 3 years ago • 2 comments

Summary

There's currently no way to allow ELSE statements on objects with two or more property constraints, take this object as an example:

type Person {
  required property email -> str {
     constraint exclusive;
  }
  required property phone_number-> str {
     constraint exclusive;
  }
}

If you wanted to preform an insert with an else clause, you would have to add the UNLESS CONFLICT ON ... clause like so:

insert Person { ... } unless conflict on ??? else (...)

but since we're working with property level constraints, there's not direct way of selecting 2 or more properties for the UNLESS CONFLICT statement. This can be solved by adding an object level constraint like so:

type Person {
  required property email -> str;
  required property phone_number-> str;

  constraint exclusive on ((.email, .phone_number));
}

Another solution (and why I'm opening this github issue) is to make the [ON ...] optional. This allows us to write a working query like so:

insert Person { ... } unless conflict else (...)

This implicit solution would find whatever constraints exist on Person and apply that to the CONFLICT statement.

quinchs avatar Aug 07 '22 04:08 quinchs

Yeah, I think this should be possible.

I actually implemented this in https://github.com/edgedb/edgedb/pull/2196 and then backed it out in https://github.com/edgedb/edgedb/pull/2202.

The thing that is potentially weird about it is that unlike currently, multiple objects might need to be selected in the ELSE block. If I recall correctly, I think I wasn't too bothered by that but others were nervous? I think that probably to mitigate the weirdness, we can make it work only for select and not for dml?

msullivan avatar Aug 08 '22 15:08 msullivan

Oh, like with #4302, it has the problem that some of the conflicting objects might have a different type, also

msullivan avatar Sep 20 '22 20:09 msullivan