Does Ash support defining exclusion constraints on resources with the DSL?

Does Ash support defining exclusion constraints on resources with the DSL (maybe that’s a Postgres specific thing?).

I’m wanting to enforce non-overlapping appointment times. It may be that I need to just write custom SQL, which, I think is how it’s done in Ecto currently if you want to do it.

There is no way to do it using any of the builtin ones (like check_constraint), but you can at least manage it in the resource using custom_statements.

The example for that is here:

https://hexdocs.pm/ash_postgres/dsl-ashpostgres-datalayer.html#postgres-custom_statements

But I’ll copy it over for reference:

custom_statements do
  # the name is used to detect if you remove or modify the statement
  statement :pgweb_idx do
    up "CREATE INDEX pgweb_idx ON pgweb USING GIN (to_tsvector('english', title || ' ' || body));"

    down "DROP INDEX pgweb_idx;"
  end
end
1 Like

Keep in mind that with custom_statements sometimes you will need to manually reorder the statements in your migration, i.e if it refers to a field you are adding or something like that.

1 Like

Oh, is that so I don’t have to go manually add it into the migration file by hand? So when I generate migrations it gets inserted? That’s super helpful.

And what is this used for? Looks like building custom error strings for when there is an exclusion error?

That is used for expressing a list of exclusion constraints that exist that may raise errors, so if you add an exclusion constraint you should add the name there so you get a nice error.

1 Like

Excellent, this gets me off to a good start.