Model Validation

Design problems, such as a table without a primary key, cause problems later in the database. DbSchema finds such problems in your design in two ways:

Model checksValidation rules
Written byDbSchemaYou or your team
RunAfter every changeWhen you press Validate, or before each save
Can stop a saveNoYes, with Validate Before Save
EditionEvery edition, the free Community Edition includedDbSchema Pro
The Model checks window with a Reserved name warning on payments.method and an Orphan table hint on shipments, and the action buttons below

The design, also called the model, is your DbSchema project: the tables, columns and diagrams that you see in DbSchema. The checks and the rules read only the design. They never change the database. Database Terms explains primary key, foreign key and the other words on this page.

See what the model checks found

The model checks run by themselves after every change. You see a problem as soon as it appears.

  1. Look at the model checks button at the right of the toolbar. An orange warning sign shows the number of warnings. A green check mark means that no warnings were found, even if there are hints. The number beside the check mark then shows how many checks ran.
  2. Click the button. The Model checks window opens.
The right side of the DbSchema toolbar, with the model checks button showing a warning sign and the number 1

Hover over the button to see how many warnings and hints it counts, and how many checks passed.

The Model checks window, at the top of this page, groups the findings by check and by schema. The Message column says what is wrong and how to fix it. You can keep editing the design while this window is open. The results update after each change.

Select a finding, then use the buttons at the bottom of the window. They show icons only, so hover over a button to see its name.

  • 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 stops this check on this table, as Silence a check on one table shows.
  • Configure checks... opens the Model Validation dialog, where you switch checks on or off.

The table dialog shows a warning button beside the table name when the checks found something on that table. Click it to see the findings of this table only.

The top row of the table dialog for payments: Table Name, Virtual, Color and, at the right, the warning button of the model checks

What the model checks look for

A warning is a likely error. A hint is a suggestion.

AboutCheckLevelWhat it finds
Tables and columnsNo primary keyWarningA table without a primary key
Tables and columnsNo columnsWarningA table without columns
Tables and columnsNullable primary key columnWarningA primary key column that isn't mandatory, so it may hold NULL
Tables and columnsRepeating groupWarningThree or more numbered columns, such as phone1, phone2, phone3, which belong in a child table
Tables and columnsid outside the primary keyHintA column named id that isn't in the primary key
Tables and columnsOrphan tableHintA table with no foreign key to or from any other table
Foreign keys and indexesForeign key type mismatchWarningA foreign key column whose data type differs from the column it refers to
Foreign keys and indexesReferred columns without a keyWarningA foreign key to columns without a primary key or unique key
Foreign keys and indexesDuplicate foreign keyWarningTwo foreign keys that refer to the same columns of the same table
Foreign keys and indexesDuplicate indexWarningTwo indexes on the same columns, in the same order
Foreign keys and indexesColumn looks like a foreign keyHintA column named after the primary key of another table, with no foreign key
Foreign keys and indexesForeign key without an indexHintA foreign key whose columns are not the first columns of any index
NamesReserved nameWarningA name that DbSchema lists as a reserved word of the database
NamesName too longWarningA name longer than the database accepts
DocumentationNo commentHintA table or column without a comment. This check is off until you switch it on
Logical designRelation without a verbHintIn a logical design, a relation without a Verb or Reverse Verb tag
Logical designSub-entity without a discriminatorHintIn a logical design, a sub-entity that names no discriminator attribute

A reserved word has a special meaning in SQL, such as order. A name like that must be quoted in every statement. Some checks don't apply to every database, such as the foreign key checks for a database that has no foreign keys.

Silence a check on one table

Some warnings or hints are acceptable in your design. For example, a log table may have no link to any other table. You can stop one check on that table, and keep it on for the other tables.

  1. Open the Model checks window from the toolbar.
  2. Select the finding.
  3. Press Silence this check here.

To stop every check on the table, open the menu of the same button and choose Silence every check on the table. The checks then skip the table, its columns, its keys and its indexes.

A tag is a named piece of extra information that the design keeps on a table. DbSchema stores the silenced checks in a table tag named checks, so they are saved in the model file:

The Tags tab of the table dialog for shipments, with the tag checks set to orphan-table

The tag holds a list of silenced checks, such as orphan-table. The value off silences every check on the table. To run a check on the table again, open the table dialog and select the Tags tab. Remove the check from the checks tag, or delete the whole tag to run every check on the table again.

The ways to stop a check differ in what they cover:

ActionWhat it stops
Silence this check hereOne check on one table
Silence every check on the tableEvery check on one table
Untick Enabled on the Model checks tabOne check in every model
Untick Run Model Checks in the settingsEvery model check in every model

Switch a check on or off

A check may not fit your project at all. For example, you may not want hints about comments. Switching a check off stops it in every model.

  1. Choose Model → Model Validation.
  2. Stay on the Model checks tab.
  3. Untick Enabled next to the check.
  4. Press OK.
The Model checks tab of the Model Validation dialog: the checks grouped by Iterate Schemas, Iterate Tables and Iterate Columns, each with its Level and an Enabled box, and No comment unticked

The Level column shows Warning or Hint. A check that reads Not for PostgreSQL doesn't apply to a PostgreSQL model. Restore defaults switches every check back to its original setting.

To stop all the model checks, untick Run Model Checks in Edit → Settings → Projects. The model checks button then disappears from the toolbar.

Write your own validation rule

A rule checks something that your team agreed on. For example, it can check a naming convention, the way your team names tables and columns. Writing and running your own rules needs DbSchema Pro.

A rule is a tree of steps:

  • An Iteration selects the objects to check. These are the schemas, the tables, or the objects of each table, such as columns, indexes, foreign keys or constraints.
  • A Check is the condition that each object must meet, such as Has Primary Key or Name Matches RegExp. A RegExp is a text pattern.
  • A Filter limits the rule to some objects, for example tables whose name starts with tmp_.

The rule below, Comments, checks that each table of each schema has a comment. The check for that is Has Description:

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

Press OK to keep the rule. DbSchema saves the rule in its own configuration folder, not in the model file. The rule is available in every model that you open.

Write a check in Groovy

If the condition you need is not in the list, choose Java Groovy Check and write a short script in Groovy, a programming language. The variable unit is the object being checked, such as a table or a column. The script returns true when the object passes, and false when it fails.

Read the validation result

The Validate button at the bottom of the Model Validation dialog runs all your rules and the model checks. It opens the Validation Result window, which shows the results of your rules and of the model checks. Each row under your rule is one object that failed the rule. DbSchema counts each such row as a warning.

The Validation Result window: the Reserved name warning on payments.method, and the rule Has Description with 7 warnings, one for each table without a comment

The button with the disk icon, Save to File, 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 Projects.
  3. Tick Validate Before Save and press OK.
DbSchema Settings, the Projects page with Validate Before Save unticked and Run Model Checks ticked

If one of your rules finds a problem, DbSchema opens the Validation Result window before it saves the model. OK saves the model anyway. Cancel goes back without saving. The model checks never stop a save.

Common problems

The model checks button is gone from the toolbar

Run Model Checks is unticked. Tick it in Edit → Settings → Projects.

A table no longer shows a finding that I expect

The check is silenced on that table. Open the table dialog, go to the Tags tab, and look for the checks tag.

The Validate button is disabled

You have no rule of your own yet. Validate runs your rules, so create one first, as Write your own validation rule shows.

Next steps