Skip to content

Easy to make mistakes when defining nullable (Field() vs Field(Column()) #464

@martin-greentrax

Description

@martin-greentrax

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

Here a sample model:


class FooDB(SQLModel, table=True):
    id: int = Field(primary_key=True, nullable=False)
    datetime_1: datetime = Field(sa_column=Column(DateTime), nullable=False)
    datetime_2: datetime = Field(sa_column=Column(DateTime, nullable=False))

    string_1: str = Field(sa_column=Column(String(50)))
    string_2: str = Field(max_length=50)

Alembic creates the table as following:


    op.create_table(
        "foodb",
        sa.Column("datetime_1", sa.DateTime(), nullable=True),
        sa.Column("datetime_2", sa.DateTime(), nullable=False),
        sa.Column("string_1", sa.String(length=50), nullable=True),
        sa.Column("string_2", sqlmodel.sql.sqltypes.AutoString(length=50), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )



### Description

I had some surprises when looking closer at what data my tables actually accept vs. what I saw in my models.py. I find that it is very easy that one thinks one is doing the right thing (while looking at the pydantic/sqlmodel docs), but the definition does actually never make it into the database.

In my sample model I would have expected that the nullable-Attribute would lead to the same in each pair.
  
Related to https://github.com/tiangolo/sqlmodel/issues/420

### Operating System

Linux

### Operating System Details

_No response_

### SQLModel Version

0.0.8

### Python Version

Python 3.10.7

### Additional Context

_No response_

Metadata

Metadata

Assignees

No one assigned

    Labels

    answeredfeatureNew feature or requestquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions