FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Non-optional property with default value is not translated to a non-nullable field in sqlalchemy · Issue #76 · fastapi/sqlmodel · GitHub

Repository navigation

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

Closed

Description

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

Activity

  1. ChielWH commented on Feb 17, 2022

    Shouldn't it result in:

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

    ?

  2. skykasko commented on Feb 17, 2022

    @ChielWH: No, default values provided in SQLModel/SQLAlchemy columns through the default parameter are invoked client-side by SQLAlchemy before data is passed to the database, and are not part of the table's DDL definition. To create a server-side default value that is part of the table's definition, the server_default parameter must be used instead.

    See: https://docs.sqlalchemy.org/en/14/core/defaults.html.

  3. ChielWH commented on Feb 17, 2022

    @skykasko All clear, makes good sense though 👍

  4. added
    bugSomething isn't working
    and removed
    questionFurther information is requested
    on Aug 27, 2022
  5. tiangolo commented on Aug 27, 2022

    Member

    Thanks for the detailed report and fix @van51! 🙇 🍰

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

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


      Back | FazBrowse Home | New Git URL