agronholm/sqlacodegen

Could not assemble any primary key when using the SQLModel generator

simkessy opened this issue · 0 comments

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Sqlacodegen version

3.0.0rc5

SQLAlchemy version

2.0.30

RDBMS vendor

MySQL (or compatible)

What happened?

I generated the sqlmodel and got something like this for one table:

class ContactDetails(SQLModel, table=True):
    __tablename__ = 'contact_details'

    id: Optional[int] = Field(
        default=None, sa_column=mapped_column("id", Integer, primary_key=True)
    )

But when I try to load this model I get:

File "/usr/local/lib/python3.8/dist-packages/sqlmodel/main.py", line 553, in __init__
    DeclarativeMeta.__init__(cls, classname, bases, dict_, **kw)
  File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/decl_api.py", line 196, in __init__
    _as_declarative(reg, cls, dict_)
  File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/decl_base.py", line 244, in _as_declarative
    return _MapperConfig.setup_mapping(registry, cls, dict_, None, {})
  File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/decl_base.py", line 325, in setup_mapping
    return _ClassScanMapperConfig(
  File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/decl_base.py", line 579, in __init__
    self._early_mapping(mapper_kw)
  File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/decl_base.py", line 366, in _early_mapping
    self.map(mapper_kw)
  File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/decl_base.py", line 1954, in map
    mapper_cls(self.cls, self.local_table, **self.mapper_args),
  File "<string>", line 2, in __init__
  File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/util/deprecations.py", line 281, in warned
    return fn(*args, **kwargs)  # type: ignore[no-any-return]
  File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/mapper.py", line 853, in __init__
    self._configure_pks()
  File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/mapper.py", line 1639, in _configure_pks
    raise sa_exc.ArgumentError(
sqlalchemy.exc.ArgumentError: Mapper Mapper[ContactDetails(contact_details)] could not assemble any primary key columns for mapped table 'contact_details'

I'm using SQLModel v 0.0.19