You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 errorModel.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:
fromdjango.db.models.functionsimportLower# This should work as intendedModel.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."
The text was updated successfully, but these errors were encountered:
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
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:
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:
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."
The text was updated successfully, but these errors were encountered: