Model Validation

Model validation finds problems in your database design before they reach the database. DbSchema checks the model after every change and counts what it found in the toolbar. You can add rules of your own, such as "every table has a comment".

The Model Validation dialog with the Model checks tab: each check with its level and an Enabled box

See what the checks found

The checks run on their own, so you see a problem as soon as you make it.

  1. Look at the warning sign at the right of the toolbar. The number next to it counts the warnings.
  2. Click it. The Model checks window opens.
The right side of the toolbar with a warning sign and the number 1

The window groups the findings by check and by schema. The Message column says what is wrong and how to fix it.

The Model checks window with a Reserved name warning on payments.method and an Orphan table hint on shipments

Select a finding, then use the buttons at the bottom of the window:

  • Open editor: opens the dialog of the table or column, so you can fix it.
  • Show in diagram: shows the table on the diagram.
  • Silence this check here: this check skips the table from now on. DbSchema adds the check to the table's checks tag.
  • Silence every check on the table, in the menu of the same button: no check looks at the table, its columns, its keys or its indexes.
  • Configure checks...: opens the Model Validation dialog.

The buttons show icons. Hover over a button to see its name.

Switch a check on or off

Some checks may not fit your project. For example, a hint about tables with no comment.

  1. Choose Model → Model Validation.
  2. Stay on the Model checks tab.
  3. Untick Enabled next to the check.
  4. Press OK.

The dialog at the top of this page shows the tab. Each check has a Level: a Warning is a likely error, a Hint is a suggestion. A check that reads Not for PostgreSQL does not apply to the database of your model. Restore defaults switches every check back to how DbSchema ships it.

Write your own validation rule

A rule checks something that your team agreed on, such as a naming convention. Writing and running your own rules needs DbSchema Pro.

  1. Choose Model → Model Validation.
  2. Click the + tab and type a name for the rule, such as Comments. Press OK.
  3. Select Iterate Schemas and press Iteration. Choose Iterate Tables.
  4. Select Iterate Tables and press Check. Choose Has Description.
  5. Press Validate.

A rule is a tree of steps:

  • Iteration chooses what the rule walks through: schemas, tables, then columns, indexes, foreign keys or constraints.
  • Check is the condition that each item must meet, such as Has Primary Key or Name Matches RegExp.
  • Filter limits the rule to some items, for example tables whose name starts with tmp_.
The rule Comments: Iterate Schemas, Iterate Tables and the check Has Description

For a condition that the list does not have, choose Java Groovy Check and write a short script. The script gets the variable unit, the table or column being checked. It returns true when the item passes.

Press OK to keep the rule. DbSchema keeps the rule in its own configuration folder, not in the model file. So the rule is there for every model you open.

Read the validation result

Validate opens the Validation Result window. It lists the findings of your rules next to those of the built-in checks. Each row of your rule is one item that failed it.

The Validation Result window: the rule Has Description with 7 warnings, one for each table without a comment

The button with the disk icon saves the findings to a file.

Validate before you save

DbSchema can run your rules each time you save the model file.

  1. Choose Edit → Settings.
  2. On the General tab, select Project File.
  3. Tick Validate Before Save and press OK.
DbSchema Settings, the Project File page with Validate Before Save and Run Model Checks

When a rule fails, the Validation Result window opens before the save. OK saves the model anyway. Cancel goes back without saving. The built-in checks never block a save.

Run Model Checks on the same page turns the built-in checks on or off for every model.

Next steps