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 · 4 comments
Open
2 tasks done

deferred=True which will lead to DetachedInstanceError #815

vahidzhe opened this issue Sep 9, 2024 · 4 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?

@mmzeynalli
Copy link

@aminalaee I am not opening extra issue for now, but if I have deferred field, and I add that field (in my case created_at) to the list view, then aside from SQL that fetches data, N extra SQLs are run to get deferred field:

2025-01-26 12:00:51.975 UTC [191] LOG:  statement: BEGIN
2025-01-26 13:00:51 postgres-1       | 2025-01-26 12:00:51.975 UTC [191] LOG:  statement: SELECT core_file.created_at AS core_file_created_at 
2025-01-26 13:00:51 postgres-1       |  FROM core_file 
2025-01-26 13:00:51 postgres-1       |  WHERE core_file.url = 'https://cdn.test.com/test/portfolio/b134fd3d-f20e-4169-b361-2381e82f55fc.png'
2025-01-26 13:00:51 postgres-1       | 2025-01-26 12:00:51.987 UTC [191] LOG:  statement: ROLLBACK
2025-01-26 13:00:51 postgres-1       | 2025-01-26 12:00:51.993 UTC [191] LOG:  statement: BEGIN
2025-01-26 13:00:51 postgres-1       | 2025-01-26 12:00:51.993 UTC [191] LOG:  statement: SELECT core_file.created_at AS core_file_created_at 
2025-01-26 13:00:51 postgres-1       |  FROM core_file 
2025-01-26 13:00:51 postgres-1       |  WHERE core_file.url = 'https://cdn.test.com/test/portfolio/0abcfde2-6542-4adc-80f3-e896a0ad845d.png'
2025-01-26 13:00:51 postgres-1       | 2025-01-26 12:00:51.994 UTC [191] LOG:  statement: ROLLBACK
2025-01-26 13:00:51 postgres-1       | 2025-01-26 12:00:51.999 UTC [191] LOG:  statement: BEGIN
2025-01-26 13:00:51 postgres-1       | 2025-01-26 12:00:51.999 UTC [191] LOG:  statement: SELECT core_file.created_at AS core_file_created_at 
2025-01-26 13:00:51 postgres-1       |  FROM core_file 
2025-01-26 13:00:51 postgres-1       |  WHERE core_file.url = 'https://cdn.test.com/test/portfolio/33f76a67-b992-456e-9a46-615ecfa67940.PNG'
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.002 UTC [191] LOG:  statement: ROLLBACK
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.007 UTC [191] LOG:  statement: BEGIN
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.008 UTC [191] LOG:  statement: SELECT core_file.created_at AS core_file_created_at 
2025-01-26 13:00:52 postgres-1       |  FROM core_file 
2025-01-26 13:00:52 postgres-1       |  WHERE core_file.url = 'https://cdn.test.com/test/portfolio/00be1a01-c0e9-4c48-8b7e-99ba084b9bf2.PNG'
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.009 UTC [191] LOG:  statement: ROLLBACK
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.013 UTC [191] LOG:  statement: BEGIN
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.013 UTC [191] LOG:  statement: SELECT core_file.created_at AS core_file_created_at 
2025-01-26 13:00:52 postgres-1       |  FROM core_file 
2025-01-26 13:00:52 postgres-1       |  WHERE core_file.url = 'https://cdn.test.com/test/portfolio/f804bfd6-dc7d-4424-891e-902d8bcb5dea.PNG'
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.014 UTC [191] LOG:  statement: ROLLBACK
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.019 UTC [191] LOG:  statement: BEGIN
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.019 UTC [191] LOG:  statement: SELECT core_file.created_at AS core_file_created_at 
2025-01-26 13:00:52 postgres-1       |  FROM core_file 
2025-01-26 13:00:52 postgres-1       |  WHERE core_file.url = 'https://cdn.test.com/test/portfolio/b79c9c11-6fed-4cb2-9f25-ddb316016e82.png'
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.020 UTC [191] LOG:  statement: ROLLBACK
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.024 UTC [191] LOG:  statement: BEGIN
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.025 UTC [191] LOG:  statement: SELECT core_file.created_at AS core_file_created_at 
2025-01-26 13:00:52 postgres-1       |  FROM core_file 
2025-01-26 13:00:52 postgres-1       |  WHERE core_file.url = 'https://cdn.test.com/test/portfolio/dfb3ef6a-eebe-4e72-bde5-1276376a42f8.png'
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.026 UTC [191] LOG:  statement: ROLLBACK
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.032 UTC [191] LOG:  statement: BEGIN
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.032 UTC [191] LOG:  statement: SELECT core_file.created_at AS core_file_created_at 
2025-01-26 13:00:52 postgres-1       |  FROM core_file 
2025-01-26 13:00:52 postgres-1       |  WHERE core_file.url = 'https://cdn.test.com/user/avatar/7ef57c48-af1f-4c84-b55d-61b568d2dfea_original.jpg'
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.034 UTC [191] LOG:  statement: ROLLBACK
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.039 UTC [191] LOG:  statement: BEGIN
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.040 UTC [191] LOG:  statement: SELECT core_file.created_at AS core_file_created_at 
2025-01-26 13:00:52 postgres-1       |  FROM core_file 
2025-01-26 13:00:52 postgres-1       |  WHERE core_file.url = 'https://cdn.test.com/user/avatar/9f7173f0-161c-4441-a90b-1e5880741c3b_original.jpeg'
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.042 UTC [191] LOG:  statement: ROLLBACK
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.048 UTC [191] LOG:  statement: BEGIN
2025-01-26 13:00:52 postgres-1       | 2025-01-26 12:00:52.048 UTC [191] LOG:  statement: SELECT core_file.created_at AS core_file_created_at 
2025-01-26 13:00:52 postgres-1       |  FROM core_file 
2025-01-26 13:00:52 postgres-1       |  WHERE core_file.url = 'https://cdn.test.com/user/avatar/873d599b-cac6-4a03-a017-84353162096c_original.jpg'

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

3 participants