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."
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."