supply default for sqlmodel PK for both DB and python to work (#1788)

This commit is contained in:
Martin Xu 2023-09-08 14:51:57 -07:00 committed by GitHub
parent 891e6a4736
commit 3406be3ff8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 4 deletions

View File

@ -54,7 +54,7 @@ class Model(Base, sqlmodel.SQLModel):
"""Base class to define a table in the database."""
# The primary key for the table.
id: Optional[int] = sqlmodel.Field(primary_key=True)
id: Optional[int] = sqlmodel.Field(default=None, primary_key=True)
def __init_subclass__(cls):
"""Drop the default primary key field if any primary key field is defined."""

View File

@ -1,3 +1,4 @@
from typing import Optional
from unittest import mock
import pytest
@ -20,7 +21,7 @@ def model_default_primary() -> Model:
class ChildModel(Model):
name: str
return ChildModel(name="name") # type: ignore
return ChildModel(name="name")
@pytest.fixture
@ -32,10 +33,10 @@ def model_custom_primary() -> Model:
"""
class ChildModel(Model):
custom_id: int = sqlmodel.Field(default=None, primary_key=True)
custom_id: Optional[int] = sqlmodel.Field(default=None, primary_key=True)
name: str
return ChildModel(name="name") # type: ignore
return ChildModel(name="name")
def test_default_primary_key(model_default_primary):