Skip to content

Conversation

sheinbergon
Copy link
Collaborator

Fixes #412

Looks like both declarative and sqlmodels generators were omitting non-nullability from the column definition

Checklist

If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):

  • You've added tests (in tests/) which would fail without your patch
  • You've added a new changelog entry (in CHANGES.rst).

If this is a trivial change, like a typo fix or a code reformatting, then you can ignore
these instructions.

Updating the changelog

If there's no issue linked, just link to your pull request instead by updating the
changelog after you've created the PR.

@sheinbergon
Copy link
Collaborator Author

@agronholm can you please have a look? I'd like to release 3.1 after this is merged.

if is_primary:
kwargs["primary_key"] = True
if not column.nullable and not is_sole_pk and is_table:
if not column.nullable and not column.primary_key:
Copy link
Owner

Choose a reason for hiding this comment

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

It is possible for some columns of a multi-column primary key constraint to be NULL, though this is exceedingly rare in practice. But this is why I had the condition in place. Perhaps we could simply drop the is_table condition?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmmmm. I do understand this concern. However, this resulted in a somewhat weird behavior where some composite primary keys columns were getting the nullability flag. Maybe I misunderstood what I was seeing. I'll return this flag and check again

Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure we have a test covering the case of partially nullable PK, but it might be worth adding one if we don't have it already.

Copy link
Collaborator Author

@sheinbergon sheinbergon Aug 9, 2025

Choose a reason for hiding this comment

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

Ok, I realized what was bugging me. I've improved the conditioning and added a test to cover exactly the case you mentioned

@sheinbergon sheinbergon force-pushed the sheinbergon/explicit_nullability_definition branch from 3756a3c to 8b6794b Compare August 5, 2025 11:33
@coveralls
Copy link

coveralls commented Aug 5, 2025

Coverage Status

coverage: 97.671% (+0.008%) from 97.663%
when pulling eede84b on sheinbergon:sheinbergon/explicit_nullability_definition
into 2965556 on agronholm:master.

@sheinbergon sheinbergon requested a review from agronholm August 9, 2025 20:46
@sheinbergon sheinbergon merged commit 4b37d5d into agronholm:master Aug 10, 2025
8 checks passed
@sheinbergon sheinbergon deleted the sheinbergon/explicit_nullability_definition branch August 10, 2025 18:14
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.

Omission of nullable= in --sqlmodel mode breaks schema generation in SQLite and PostgreSQL
3 participants