Skip to content

Definition syntax: string defaults and when NULL applies - #323

Open
dimitri-yatsenko wants to merge 4 commits into
mainfrom
docs/string-null-rule
Open

dimitri-yatsenko wants to merge 4 commits into
mainfrom
docs/string-null-rule

Conversation

@dimitri-yatsenko

@dimitri-yatsenko dimitri-yatsenko commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Restates the string-default rule on the definition-syntax reference page (Defaults).

  • Declare an optional varchar, char, or enum (one with a default) with the default ''; DataJoint never supplies '' itself. An enum that may be empty includes '' in its values, and a default of '' that is not among them is rejected.
  • A required string has no default, so an insert that omits it fails.
  • A string or enum may be declared NULL when missing must be told apart from empty: NULL means the value is missing, and '' means it is known to be empty. An example shows the declaration and the restrictions that read each case.
  • Use varchar for free text and char(n) for fixed-width codes: on PostgreSQL char(n) pads '' to n spaces, while restrictions give the same results on both backends.

The previous text allowed the same exception but described it as rare. This version states when it applies and what each value means.

Checks

mkdocs build --strict, scripts/check_markdown_lists.py, and codespell pass.

…uthor

Rephrase the empty-string rule as an instruction and define optional, write
the NULL and '' tests as restrictions, show the reading side of the consent
example, note that an enum default of '' must be among its values, and
recommend varchar over char(n), which pads with spaces on PostgreSQL.

@MilagrosMarin MilagrosMarin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dimitri-yatsenko, this turns the old "rare" exception into a clear rule. I ran the examples on MySQL 8.0 and PostgreSQL 16 with 2.3.6 and current master, and almost everything matches the text:

  • An enum default of '' that is not among its values is rejected when the table is declared. The error comes from the database (MySQL 1067 "Invalid default value for 'stage'"; PostgreSQL "invalid input value for enum ..."), so "rejected by the database when the table is declared" would tell readers what to expect.
  • An insert that omits a required string fails on both backends, as does passing None for it.
  • Consent & "consent_note IS NULL" and Consent & {"consent_note": ""} return exactly the NULL row and the empty row on both backends.

One sentence needs narrowing:

  • "Restrictions in queries give the same results on both backends" holds for equality and IN (string or dict), but not for LIKE or other expressions on the column. With code = 'ab' in a char(3) column, code LIKE 'ab' and concat(code, 'z') = 'abz' match on MySQL and return nothing on PostgreSQL. Fetched values differ too ('ab' vs 'ab '), so comparisons in make() behave differently. The padding also applies to any shorter value, not only ''. Suggested: "On PostgreSQL, a char(n) column pads shorter values with spaces ('' reads back as n spaces, 'ab' in char(3) as 'ab '), while MySQL does not. Equality and IN restrictions match on both backends; LIKE patterns and comparisons on fetched values do not."

Optional:

  • Passing None for a non-nullable string with a '' default stores '' (the declared default), not NULL. A clause after "an attribute gets the default its definition declares" would head off that surprise.

With the char(n) sentence scoped to equality restrictions, this section holds on both backends.

…t rule

Scope the cross-backend claim to equality and IN restrictions, since
PostgreSQL pads char(n) values and LIKE patterns, expressions, and fetched
values differ. Say the database rejects an enum default of '' that is not
among its values. Note that None for a non-nullable string stores its
declared default. Recast the rule as '' rather than NULL so it does not
discourage non-empty defaults.
@dimitri-yatsenko

Copy link
Copy Markdown
Member Author

Thanks @MilagrosMarin for running these on both backends. All three points are now in:

  • The char(n) sentence uses your suggested text: PostgreSQL pads shorter values, equality and IN restrictions match on both backends, and LIKE patterns, other expressions on the column, and comparisons on fetched values do not.
  • The enum case now says the database rejects a '' default that is not among the values, when the table is declared.
  • Added the clause you suggested: passing None for an attribute that is not nullable inserts its declared default, so None for a ''-default string stores '', not NULL.

I also reworded the bold rule as "use '' rather than NULL" so it does not read as discouraging non-empty defaults like "pending".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants