Skip to content

Default values for fields not working as expected with SQLModel in PostgreSQL #1260

Open
@Yoosuph

Description

@Yoosuph

Privileged issue

  • I'm @tiangolo or he asked me directly to create an issue here.

Issue Content

I'm encountering an issue where default values for fields in my SQLModel class are not being applied when using PostgreSQL. Despite specifying default or default_factory for various fields, the database does not seem to handle these defaults, and errors occur during table creation or insertion.

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

class Posts(SQLModel, table=True):
id: Optional[int] = Field(default_factory=int, primary_key=True)
title: str
content: str
published: Optional[bool] = Field(default_factory=lambda: True)
created_at: Optional[datetime] = Field(default_factory=datetime.now)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions