from typing import Optional

from sqlalchemy import create_engine, select
from sqlalchemy.orm import Session
from sqlmodel import Field, SQLModel


def test_allow_instantiation_without_arguments(clear_sqlmodel):
    class Item(SQLModel):
        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:
        item = Item()
        item.name = "Rick"
        db.add(item)
        db.commit()
        result = db.execute(select(Item)).scalars().all()
    assert len(result) == 1
    assert isinstance(item.id, int)
    SQLModel.metadata.clear()

sqlmodel.SQLModel.metadata.create_all sqlalchemy.select sqlmodel.Field sqlalchemy.orm.Session sqlalchemy.create_engine sqlmodel.SQLModel.metadata.clear
[((426, 461), 'sqlalchemy.create_engine', 'create_engine', (['"""sqlite:///:memory:"""'], {}), "('sqlite:///:memory:')\n", (439, 461), False, 'from sqlalchemy import create_engine, select\n'), ((466, 502), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (494, 502), False, 'from sqlmodel import Field, SQLModel\n'), ((751, 776), 'sqlmodel.SQLModel.metadata.clear', 'SQLModel.metadata.clear', ([], {}), '()\n', (774, 776), False, 'from sqlmodel import Field, SQLModel\n'), ((266, 303), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (271, 303), False, 'from sqlmodel import Field, SQLModel\n'), ((512, 527), 'sqlalchemy.orm.Session', 'Session', (['engine'], {}), '(engine)\n', (519, 527), False, 'from sqlalchemy.orm import Session\n'), ((653, 665), 'sqlalchemy.select', 'select', (['Item'], {}), '(Item)\n', (659, 665), False, 'from sqlalchemy import create_engine, select\n')]