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 checks | Validation rules | |
|---|---|---|
| Written by | DbSchema | You or your team |
| Run | After every change | When you press Validate, or before each save |
| Can stop a save | No | Yes, with Validate Before Save |
| Edition | Every edition, the free Community Edition included | DbSchema Pro |
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.
- 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.
- Click the button. The Model checks window opens.
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.
What the model checks look for
A warning is a likely error. A hint is a suggestion.
| About | Check | Level | What it finds |
|---|---|---|---|
| Tables and columns | No primary key | Warning | A table without a primary key |
| Tables and columns | No columns | Warning | A table without columns |
| Tables and columns | Nullable primary key column | Warning | A primary key column that isn't mandatory, so it may hold NULL |
| Tables and columns | Repeating group | Warning | Three or more numbered columns, such as phone1, phone2, phone3, which belong in a child table |
| Tables and columns | id outside the primary key | Hint | A column named id that isn't in the primary key |
| Tables and columns | Orphan table | Hint | A table with no foreign key to or from any other table |
| Foreign keys and indexes | Foreign key type mismatch | Warning | A foreign key column whose data type differs from the column it refers to |
| Foreign keys and indexes | Referred columns without a key | Warning | A foreign key to columns without a primary key or unique key |
| Foreign keys and indexes | Duplicate foreign key | Warning | Two foreign keys that refer to the same columns of the same table |
| Foreign keys and indexes | Duplicate index | Warning | Two indexes on the same columns, in the same order |
| Foreign keys and indexes | Column looks like a foreign key | Hint | A column named after the primary key of another table, with no foreign key |
| Foreign keys and indexes | Foreign key without an index | Hint | A foreign key whose columns are not the first columns of any index |
| Names | Reserved name | Warning | A name that DbSchema lists as a reserved word of the database |
| Names | Name too long | Warning | A name longer than the database accepts |
| Documentation | No comment | Hint | A table or column without a comment. This check is off until you switch it on |
| Logical design | Relation without a verb | Hint | In a logical design, a relation without a Verb or Reverse Verb tag |
| Logical design | Sub-entity without a discriminator | Hint | In 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.
- Open the Model checks window from the toolbar.
- Select the finding.
- 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 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:
| Action | What it stops |
|---|---|
| Silence this check here | One check on one table |
| Silence every check on the table | Every check on one table |
| Untick Enabled on the Model checks tab | One check in every model |
| Untick Run Model Checks in the settings | Every 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.
- Choose Model → Model Validation.
- Stay on the Model checks tab.
- Untick Enabled next to the check.
- Press OK.
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:
- Choose Model → Model Validation.
- Click the + tab, type a name for the rule, such as
Comments, and press OK. - Select Iterate Schemas and press Iteration. Choose Iterate Tables and press Select.
- Select Iterate Tables and press Check. Choose Has Description and press Select.
- Press Validate.
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 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.
- Choose Edit → Settings.
- On the General tab, select Projects.
- Tick Validate Before Save and press OK.
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.