Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot create new row when a column contains a table that has Polymorphic Inheritance #866

Open
2 tasks done
AlbertMitjans opened this issue Dec 20, 2024 · 0 comments
Open
2 tasks done

Comments

@AlbertMitjans
Copy link

Checklist

  • The bug is reproducible against the latest release or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

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: sqlalchemy/sqlalchemy#12157 (reply in thread)

Steps to reproduce the bug

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.

Expected behavior

No response

Actual behavior

No response

Debugging material

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>))

Environment

  • Python 3.11
  • Sqladmin 0.16.1

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant