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

custom aggregated column does not work with nullable=False #715

Open
omer9564 opened this issue Sep 7, 2023 · 0 comments
Open

custom aggregated column does not work with nullable=False #715

omer9564 opened this issue Sep 7, 2023 · 0 comments

Comments

@omer9564
Copy link

omer9564 commented Sep 7, 2023

I have two ORM models:

  1. Component
  2. Inventory

There is a one to many relationship between Component to Inventory

Component has a custom aggregated column named quantity defined as

@aggregated(
        "inventories", mapped_column(Integer, nullable=False, server_default="0")
    )
    def quantity(self) -> Mapped[int]:
        return func.sum(Inventory.quantity)

I have a component with id component-1 and 2 inventories with id inventory-1 and inventory-2
when I delete both of them, the package generates the following query:

UPDATE components SET quantity=(SELECT sum(inventories.quantity) AS sum_1
                                         FROM inventories
                                         WHERE components.id = inventories.component_id), updated_at=now() WHERE components.id IN ('component-1')

This query results on trying to set null in the quantity column because there are no inventories that match the where clause

Couldn't find anything in the documentation regarding this or open issue about this issue.

Using:

  • python 3.10
  • SQLAlchemy 2
  • sqlalchemy-utils 0.41.1
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