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

Bug: dictionary changed size during iteration #507

Open
foarsitter opened this issue Feb 14, 2024 · 7 comments · May be fixed by #508
Open

Bug: dictionary changed size during iteration #507

foarsitter opened this issue Feb 14, 2024 · 7 comments · May be fixed by #508
Labels
bug Something isn't working

Comments

@foarsitter
Copy link

foarsitter commented Feb 14, 2024

Somehow my models aren't completely loaded when the admin is initialized. During the setup of the admin fields are added to the model and therefore self.model.__dict__ changed size during iteration.

Fixing the problem is easier than isolating a test case, so I do not have something to reproduce the error.

  • Starlette-Admin version: 0.13.2
  • ORM/ODMs: SQLAlchemy
@foarsitter foarsitter added the bug Something isn't working label Feb 14, 2024
@foarsitter foarsitter linked a pull request Feb 14, 2024 that will close this issue
@jowilf
Copy link
Owner

jowilf commented Feb 16, 2024

Without a minimal reproducible example, we can't ensure that PR #508 effectively fixed the issue.

@foarsitter
Copy link
Author

For now you have to believe me that this fixed my project :) For the foreseeable future I will update the PR but not in upcoming week.

@hasansezertasan
Copy link
Contributor

For now you have to believe me that this fixed my project :) For the foreseeable future I will update the PR but not in upcoming week.

I think you should provide an example use case.

@foarsitter
Copy link
Author

I'm unable to find the root cause and thus cannot provide a failing test case.

@nkhitrov
Copy link

I have the same error with association_proxy

class PreparationPhaseDB(MappedAsDataclass, BaseDB):
    __tablename__ = "preparation_phases"
    
	attachments: AssociationProxy[list[AttachmentDB]] = association_proxy(  # problem attr 
	        "attachments_relationship", "attachment", init=False
	    )
	 attachments_relationship: Mapped[list[PreparationPhaseAttachmentRelationDB]] = (
        relationship(lazy="noload", init=False)
    )
    
    
admin = Admin(engine=engine, ...)
admin.add_view(ModelView(PreparationPhaseDB, name="phases"))
 File "/Users/khitrov/Projects/ExamSimulator/exam-simulator-backend/exam_simulator/admin/admin.py", line 37, in create_admin_app
    admin.add_view(ModelView(PreparationPhaseDB, name="phases"))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/khitrov/.pyenv/versions/3.11.6/envs/exam-simulator/lib/python3.11/site-packages/starlette_admin/contrib/sqla/view.py", line 104, in __init__
    self._setup_primary_key()
  File "/Users/khitrov/.pyenv/versions/3.11.6/envs/exam-simulator/lib/python3.11/site-packages/starlette_admin/contrib/sqla/view.py", line 137, in _setup_primary_key
    for key in self.model.__dict__:
RuntimeError: dictionary changed size during iteration

@nkhitrov
Copy link

I change loop code

for key in self.model.__dict__.keys:

to

for key in list(self.model.__dict__.keys()):

and now it just works

@jowilf
Copy link
Owner

jowilf commented Sep 19, 2024

Great, thanks for sharing the fix. feel free to submit PR with your example as a test case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants