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

deferred=True which will lead to DetachedInstanceError #815

Open
2 tasks done
vahidzhe opened this issue Sep 9, 2024 · 3 comments
Open
2 tasks done

deferred=True which will lead to DetachedInstanceError #815

vahidzhe opened this issue Sep 9, 2024 · 3 comments

Comments

@vahidzhe
Copy link

vahidzhe commented Sep 9, 2024

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

created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        server_default=func.now(tz=pytz.timezone('Asia/Baku')),
        deferred=True,
    )
updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        server_default=func.now(tz=pytz.timezone('Asia/Baku')),
        onupdate=func.now(tz=pytz.timezone('Asia/Baku')),
        deferred=True,
    )

In my code, when deferred=True in the created_at field, it is not possible to edit an instance of my model in the Admin panel.

raise orm_exc.DetachedInstanceError(
  sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <User at 0x7f215df6ce10> is not bound to a Session; 
  deferred load operation of attribute 'created_at' cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3)

https://docs.sqlalchemy.org/en/20/errors.html#error-bhk3

Steps to reproduce the bug

No response

Expected behavior

No response

Actual behavior

No response

Debugging material

No response

Environment

Windows 10 / ptyhon 3.11 / sqladmin 0.19

Additional context

No response

@vahidzhe vahidzhe changed the title defferred=True which will lead to DetachedInstanceError deferred=True which will lead to DetachedInstanceError Sep 9, 2024
@aminalaee
Copy link
Owner

Hi,
Just a question, what is your expected behaviour here? I'm guessing you don't want to be able edit created_at and updated_at right?

In that case you can just specify form_columns and leave out columns like created_at to not be shown.

class MyAdmin(...):
    form_columns = ["column_a"]

@vahidzhe
Copy link
Author

vahidzhe commented Sep 10, 2024

I used form_excluded_columns and it worked. Thanks

@vahidzhe vahidzhe reopened this Sep 10, 2024
@vahidzhe
Copy link
Author

@aminalaee But there may be places where it is necessary to edit the created_at field, so what can I do?

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

2 participants