# Design checks

> Every rule a database design is checked against, its level and its fix.

Every design is checked on each save against these 46 rules. Errors come first, then warnings, then advice, each with its fix. Errors never block a save: the design is kept, and the Problems list shows what to fix.

Rules marked **(off)** run only when the design turns them on. Dialect rules run when the `Project` names its `database_type`.

## Turning rules off and on

A comment on a table, column, ref, index or enum ignores a rule there. It needs a reason:

```dbml
Table books {
  id bigint [pk, increment]
  // dockito-ignore money-float -- the sum of star ratings, not money
  rating_total float
}
```

On the `Project`, or at the top of the file, it applies to the whole design. `dockito-enable` turns an off rule on, at its own level or one you give:

```dbml
// dockito-enable plural-mix warning
Project book_club { database_type: 'PostgreSQL' }
```

An ignore with no reason, or naming no rule, ignores nothing and shows as `bad-ignore`. `dockito check <design> --ignored` lists what's ignored and why.

## Structure

| Rule | Level | Dialect | What it catches | Fix |
|---|---|---|---|---|
| `no-pk` | Warning | Any | a table with no primary key | add id bigint [pk, increment], or (a, b) [pk] |
| `ref-not-unique` | Error | Any | a ref's key end isn't a primary key or unique | add [unique] on the key end, or ref the table's key |
| `ref-type` | Error | Any | a foreign key's type differs in kind from its key's (in MySQL also integer size or sign) | give the foreign key its key's type |
| `ref-int-size` | Warning | PostgreSQL | a foreign key's integer size differs from its key's | give the foreign key its key's type |
| `ref-length` | Warning | Any | a foreign key's length or scale differs from its key's | give the foreign key its key's length |
| `ref-order` | Error | MySQL | a composite ref's key columns are a key in another order | write the ref in the key's order |
| `name-clash` | Error | PostgreSQL, MySQL | two names the database keeps in one place, or names that differ only in case | rename one of them |
| `reserved-word` | Warning | PostgreSQL, MySQL | a table or column named with a reserved word | rename it (users, app_user) |
| `name-too-long` | Warning (MySQL: error) | PostgreSQL, MySQL | a name, or one the SQL makes, past the dialect's length | shorten it, or name the index |
| `name-quotes` | Warning | Any | a name that must be quoted: spaces, dashes, a leading digit, non-ASCII; in Postgres capitals | rename it in snake_case |
| `unknown-type` | Warning | PostgreSQL, MySQL | a type that isn't the dialect's nor an enum in the design | use the dialect's type |
| `bad-default` | Error | Any | a default that doesn't fit its type or enum | give a default of the column's type |
| `bad-increment` | Error | Any | increment on a non-integer; in MySQL two in a table, or one off a key | make it an integer key, or drop increment |
| `my-key-too-long` | Error | MySQL | a key on text or blob, or an index past 3,072 bytes | use varchar(191), or index a prefix |

## Integrity

| Rule | Level | Dialect | What it catches | Fix |
|---|---|---|---|---|
| `set-null-not-null` | Warning (MySQL: error) | PostgreSQL, MySQL | set null on a not null foreign key | drop not null, or use cascade or restrict |
| `set-default` | Warning (MySQL: error) | PostgreSQL, MySQL | set default in MySQL, or on a column with no default in Postgres | add a default, or use restrict |
| `one-to-one` | Warning | Any | a one-to-one ref whose foreign key isn't unique | add [unique] on the foreign key, or write > |
| `ref-cycle` | Warning | Any | a cycle of not null foreign keys | make one of them nullable |
| `missing-ref` | Advice | Any | a column named as another table's key, with no ref | add the ref |
| `orphan-table` (off) | Advice | Any | a table with no refs, in a design of 5 or more | add a ref to it, or drop it |

## Performance

| Rule | Level | Dialect | What it catches | Fix |
|---|---|---|---|---|
| `fk-no-index` | Warning | PostgreSQL | a foreign key with no index leading with it | add an index on the foreign key |
| `duplicate-index` | Warning | Any | two indexes on the same columns, or one on the key's | drop the extra index |
| `redundant-index` | Advice | Any | a plain index whose columns lead another index | drop it: the longer index covers it |
| `wide-pk` | Advice | Any | a key over text, a long varchar, 3+ columns, or (MySQL) a char(36) uuid | add id bigint [pk, increment]; keep the rest [unique] |
| `int-pk` | Advice | Any | an int key that increments | make it bigint |
| `wide-table` | Advice | Any | a table of more than 60 columns | split the rarely used columns into a table joined 1-1 |

## Naming

| Rule | Level | Dialect | What it catches | Fix |
|---|---|---|---|---|
| `case-style` (off) | Advice | Any | names mixing snake_case, camelCase and PascalCase | rename it as the others |
| `plural-mix` (off) | Advice | Any | table names mixing plural and singular | rename it as the others |
| `pk-name` (off) | Advice | Any | keys named differently across tables | rename it as the others |
| `fk-name` (off) | Advice | Any | a foreign key that doesn't name the table it points at | rename it to name its table |
| `same-name-type` | Advice | Any | one column name with different types across tables | give it one type |

## Types

| Rule | Level | Dialect | What it catches | Fix |
|---|---|---|---|---|
| `money-float` | Warning | Any | a float on a money-like column | use numeric(12,2), or bigint in cents |
| `pg-money` | Advice | PostgreSQL | Postgres's money type | use numeric(12,2) and a currency column |
| `pg-timestamp` | Advice | PostgreSQL | timestamp without time zone | use timestamptz |
| `my-timestamp` | Advice | MySQL | MySQL's timestamp, which ends in 2038 | use datetime, kept in UTC |
| `my-varchar` | Error | MySQL | varchar with no length | use varchar(255), or text |
| `pg-serial` | Advice | PostgreSQL | serial or bigserial | write bigint [pk, increment] |
| `pg-char` | Advice | PostgreSQL | char(n), which pads with spaces | use varchar(n) or text |
| `pg-varchar` (off) | Advice | PostgreSQL | varchar(n) where text would do | use text, with a check if a limit matters |
| `enum-big` | Advice | Any | an enum of more than 20 values, or (MySQL) used by 4+ tables | make a lookup table and ref it |

## Smells

| Rule | Level | Dialect | What it catches | Fix |
|---|---|---|---|---|
| `many-to-many` | Advice | Any | a many-to-many ref | add a join table with two > refs |
| `polymorphic` | Advice | Any | a x_type and x_id pair with no ref | use a nullable ref a target table, or a join table a target |
| `numbered-columns` | Advice | Any | columns numbered as a list (phone1, phone2) | make a table for them |
| `all-nullable` (off) | Advice | Any | every column but the key nullable | add not null where a value is required |
| `no-dialect` | Advice | Any | no database_type on the Project: dialect rules don't run | add Project &#123; database_type: 'PostgreSQL' } |
| `bad-ignore` | Advice | Any | a dockito-ignore with no reason, or naming no rule | write // dockito-ignore &lt;rule> -- &lt;reason> |
