supply default for sqlmodel PK for both DB and python to work (#1788)
This commit is contained in:
parent
891e6a4736
commit
3406be3ff8
@ -54,7 +54,7 @@ class Model(Base, sqlmodel.SQLModel):
|
|||||||
"""Base class to define a table in the database."""
|
"""Base class to define a table in the database."""
|
||||||
|
|
||||||
# The primary key for the table.
|
# 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):
|
def __init_subclass__(cls):
|
||||||
"""Drop the default primary key field if any primary key field is defined."""
|
"""Drop the default primary key field if any primary key field is defined."""
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
from typing import Optional
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@ -20,7 +21,7 @@ def model_default_primary() -> Model:
|
|||||||
class ChildModel(Model):
|
class ChildModel(Model):
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
return ChildModel(name="name") # type: ignore
|
return ChildModel(name="name")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@ -32,10 +33,10 @@ def model_custom_primary() -> Model:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
class ChildModel(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
|
name: str
|
||||||
|
|
||||||
return ChildModel(name="name") # type: ignore
|
return ChildModel(name="name")
|
||||||
|
|
||||||
|
|
||||||
def test_default_primary_key(model_default_primary):
|
def test_default_primary_key(model_default_primary):
|
||||||
|
Loading…
Reference in New Issue
Block a user