Skip to content

Non-optional property with default value is not translated to a non-nullable field in sqlalchemy #76

@van51

Description

@van51

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

from typing import Optional
from sqlmodel import SQLModel, Field, create_engine


class Test(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    a: str
    b: Optional[str] = None
    c: str = Field(default="Hey")
    d: str = "hey"
    
sqlite_url = ""  # define a path here
engine = create_engine(sqlite_url, echo=True)
SQLModel.metadata.create_all(engine)

Description

The above code example issues the following create table sql command:

CREATE TABLE test (
	id INTEGER, 
	a VARCHAR NOT NULL, 
	b VARCHAR, 
	c VARCHAR, 
	d VARCHAR, 
	PRIMARY KEY (id)
)

whereas I would expect columns c and d to be marked as NOT NULL as well.
I think the documentation also points to my point of view as stated here.

Operating System

Linux

Operating System Details

No response

SQLModel Version

0.0.4

Python Version

3.8.5

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions