master
.I have the following SQLAlchemy models:
from uuid import UUID, uuid4
from sqlalchemy import ForeignKey, String, inspect
from sqlalchemy.dialects.postgresql import UUID as PSQL_UUID
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
pass
class Interlocutor(Base):
__tablename__ = "interlocutors"
__mapper_args__ = {"polymorphic_on": "type", "polymorphic_identity": "interlocutor"}
# Base primary key
uuid: Mapped[UUID] = mapped_column(
PSQL_UUID(as_uuid=True),
primary_key=True,
default=uuid4,
)
type: Mapped[str] = mapped_column(String, nullable=False)
class AIAgent(Interlocutor):
__tablename__ = "ai_agents"
__mapper_args__ = {"polymorphic_identity": "ai_agent"}
uuid: Mapped[UUID] = mapped_column(
PSQL_UUID(as_uuid=True),
ForeignKey("interlocutors.uuid"),
primary_key=True,
default=uuid4,
)
class Token(BaseModel):
__tablename__ = "tokens"
key: Mapped[str] = mapped_column(String, nullable=False)
agent_id: Mapped[UUID] = mapped_column(PSQLUUID(), ForeignKey("ai_agents.uuid"), nullable=False)
agent: Mapped["AuthUser"] = relationship(back_populates="tokens")
Then I create a view for the Token
table.
When trying to create a new token, it fails with the following error (see full traceback below):
Column('uuid', UUID(), ForeignKey('chats.interlocutors.uuid'), table=<auth_users>, primary_key=True, nullable=False, default=CallableColumnDefault(<function uuid4 at 0x12ee09d00>))
The guys from SQLAlchemy provided a solution to this bug: https://github.com/sqlalchemy/sqlalchemy/discussions/12157#discussioncomment-11464233
Take the code above, create a view for the Token table and try to create a new row for that table using the admin panel.
No response
No response
Column('uuid', UUID(), ForeignKey('chats.interlocutors.uuid'), table=<auth_users>, primary_key=True, nullable=False, default=CallableColumnDefault(<function uuid4 at 0x12ee09d00>))
Traceback (most recent call last):
File "/Users/albertmitjans/Library/Caches/pypoetry/virtualenvs/sellebrate-fiX1CKzp-py3.12/lib/python3.12/site-packages/sqladmin/application.py", line 520, in create
obj = await model_view.insert_model(request, form_data_dict)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/albertmitjans/Library/Caches/pypoetry/virtualenvs/sellebrate-fiX1CKzp-py3.12/lib/python3.12/site-packages/sqladmin/models.py", line 967, in insert_model
return await Query(self).insert(data, request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/albertmitjans/Library/Caches/pypoetry/virtualenvs/sellebrate-fiX1CKzp-py3.12/lib/python3.12/site-packages/sqladmin/_queries.py", line 226, in insert
return await anyio.to_thread.run_sync(self._insert_sync, data, request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/albertmitjans/Library/Caches/pypoetry/virtualenvs/sellebrate-fiX1CKzp-py3.12/lib/python3.12/site-packages/anyio/to_thread.py", line 33, in run_sync
return await get_asynclib().run_sync_in_worker_thread(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/albertmitjans/Library/Caches/pypoetry/virtualenvs/sellebrate-fiX1CKzp-py3.12/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 877, in run_sync_in_worker_thread
return await future
^^^^^^^^^^^^
File "/Users/albertmitjans/Library/Caches/pypoetry/virtualenvs/sellebrate-fiX1CKzp-py3.12/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 807, in run
result = context.run(func, *args)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/albertmitjans/Library/Caches/pypoetry/virtualenvs/sellebrate-fiX1CKzp-py3.12/lib/python3.12/site-packages/sqladmin/_queries.py", line 197, in _insert_sync
obj = self._set_attributes_sync(session, obj, data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/albertmitjans/Library/Caches/pypoetry/virtualenvs/sellebrate-fiX1CKzp-py3.12/lib/python3.12/site-packages/sqladmin/_queries.py", line 96, in _set_attributes_sync
obj = self._set_many_to_one(obj, relation, value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/albertmitjans/Library/Caches/pypoetry/virtualenvs/sellebrate-fiX1CKzp-py3.12/lib/python3.12/site-packages/sqladmin/_queries.py", line 69, in _set_many_to_one
setattr(obj, fk.name, pk_value[pk])
~~~~~~~~^^^^
KeyError: Column('uuid', UUID(), ForeignKey('chats.interlocutors.uuid'), table=<auth_users>, primary_key=True, nullable=False, default=CallableColumnDefault(<function uuid4 at 0x12ee09d00>))
No response
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too