Introducing DBML Check Constraints — Enforce Data Rules with Ease

Great news! We’ve added support for Check constraints, allowing you to set rules on your table fields to enforce data quality—such as ensuring quantities are above zero or ratings stay within a range.

Here’s a simple DBML example:

Table orders {
  id integer [pk, increment]
  price decimal [not null, check: `price > 0`]
  discount decimal [not null, default: 0, check: `discount >= 0`]

  checks {
    `discount < price` [name: 'valid_discount']
  }
}

Previously, these had to go in notes, which meant they could disappear during SQL imports or exports. Now, define them right in DBML, import and export with accuracy, and view them in your ER diagrams and docs for clearer sharing.


It's a simple way to make your databases more reliable and your documentation more informative. Update your diagrams now and start applying `Check` constraints!

:books: Read the full docs →

If you have any feedback for us, feel free to reply in this post.
From dbx team with :heart:

You can also use Check constraints to validate table field values in RunSQL. :point_right: Check the query here


Happy to see this new feature !

I’ve personnaly a request about it :
Is it possible to make it work with enums ? like something like this :

enum my_enum {
  value1,
  value2
}
field varchar [check: my_enum]

and it generate

field varchar CHECK(field in ('value1', 'value2'))

It can be very usefull to factorize some

Hi @Matteo_Convert ,

Thank you for the enum check suggestion. We’ll consider it in the future.

In the meantime, have you tried using the enum type directly as a field’s type to see if it works for you? For example:

enum my_enum {
  value1
  value2
}

table a {
field my_enum
}

Sorry I didn’t see that you had responded, Yes but the result produced is not what we expected once the database was generated with dotnet for example, because it does not manage postgre enums. This is why we are currently reworking the file generated by dbdiagram to ensure that they are transformed into check constraints.

Hi @Matteo_Convert,

I suppose you mean to say that because .NET cannot manage Postgres enums, so when you export DBML to postgres containing enums, it wouldn’t work with .NET properly?

This is an interesting edge case though. We can consider it in the future. However currently, it’s not obvious how to resolve this edge case properly so this may take a while.

Thank you!

For now, yes, it’s my problem, but I still think it’s a good way to use it—that’s why I suggested it; if it were only for .NET, I wouldn’t have proposed it.

To be more precise, the .NET version can handle them, but for some reason I don’t know, they aren’t generated automatically.

1 Like