Add support for Pydantic v2 (while keeping support for v1 if v2 is not available), including initial work by AntonDeMeester (#722)

Co-authored-by: Mohamed Farahat <farahats9@yahoo.com>
Co-authored-by: Stefan Borer <stefan.borer@gmail.com>
Co-authored-by: Peter Landry <peter.landry@gmail.com>
Co-authored-by: Anton De Meester <antondemeester+github@gmail.com>
This commit is contained in:
Sebastián Ramírez
2023-12-04 15:42:39 +01:00
committed by GitHub
parent 5b733b348d
commit fa2f178b8a
79 changed files with 2614 additions and 517 deletions

View File

@@ -1,19 +1,16 @@
from typing import Optional
from sqlalchemy import create_engine, select
from sqlalchemy.orm import Session
from sqlmodel import Field, SQLModel
import pytest
from pydantic import ValidationError
from sqlmodel import Field, Session, SQLModel, create_engine, select
def test_allow_instantiation_without_arguments(clear_sqlmodel):
class Item(SQLModel):
class Item(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
description: Optional[str] = None
class Config:
table = True
engine = create_engine("sqlite:///:memory:")
SQLModel.metadata.create_all(engine)
with Session(engine) as db:
@@ -21,7 +18,18 @@ def test_allow_instantiation_without_arguments(clear_sqlmodel):
item.name = "Rick"
db.add(item)
db.commit()
result = db.execute(select(Item)).scalars().all()
statement = select(Item)
result = db.exec(statement).all()
assert len(result) == 1
assert isinstance(item.id, int)
SQLModel.metadata.clear()
def test_not_allow_instantiation_without_arguments_if_not_table():
class Item(SQLModel):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
description: Optional[str] = None
with pytest.raises(ValidationError):
Item()