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

Support DB Functions in on_conflict for Expression Fields #253

Open
anupsingh3292 opened this issue Nov 16, 2024 · 0 comments
Open

Support DB Functions in on_conflict for Expression Fields #253

anupsingh3292 opened this issue Nov 16, 2024 · 0 comments

Comments

@anupsingh3292
Copy link

anupsingh3292 commented Nov 16, 2024

We encountered an issue with unique field constraints that include expressions (e.g., LOWER(field)) in PostgreSQL while using this library.

Currently, the on_conflict condition only allows passing field names but does not support expressions created using Django model functions like Lower(field) or Upper(field). This limitation causes errors when trying to insert records under such constraints, as Postgres fails to handle these expressions in the conflict resolution process.

Example:

# Code that throws an error
Model.psql_objects.on_conflict(["field", "lower_field"], DO_NOTHING).bulk_insert(rows)

The above code attempts to resolve a unique constraint using the expression LOWER(field). However, it fails because the library does not allow defining expressions like Lower(field) in the on_conflict condition.

Expected Behavior:
The library should allow passing Django model functions (e.g., Lower(field), Upper(field), etc.) in the on_conflict condition. This would enable seamless handling of unique constraints with expressions and make the library more flexible.

Suggested Fix:
Enhance the on_conflict condition to support Django model functions for fields. For example:

from django.db.models.functions import Lower

# This should work as intended
Model.psql_objects.on_conflict([field, Lower(field)], DO_NOTHING).bulk_insert(rows)

This improvement would align the library's functionality with PostgreSQL's capabilities, allowing expression-based unique constraints to work without errors

"I would be happy to contribute and work on fixing this issue if you give me the go-ahead."

@anupsingh3292 anupsingh3292 changed the title Support Django Model Functions in on_conflict for Expression Fields Support DB Functions in on_conflict for Expression Fields Nov 16, 2024
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