Open ArthurNRL opened 2 years ago
The full query:
INSERT INTO "statement_ownergroup_customer_user_group" ("ownergroup_id", "customerusergroup_id") VALUES ('2df4b23614514f93847b45c74ff2ce84', 'cf6d2a7cc06846e488905f8ed8844822') ON CONFLICT DO NOTHING
Managed to fix it doing, although I'm not database savvy enough to know if this is safe or not
from django_redshift_backend.base import DatabaseOperations as RSDatabaseOperations, DatabaseWrapper as RSDatabaseWrapper
class DatabaseOperations(RSDatabaseOperations):
def ignore_conflicts_suffix_sql(self, ignore_conflicts=None):
return ''
class DatabaseWrapper(RSDatabaseWrapper):
def __init__(self, *args, **kwargs):
super(DatabaseWrapper, self).__init__(*args, **kwargs)
self.ops = DatabaseOperations(self)
Thanks for yoru reporting and inspection!
Upon further investigation, it appears that INSERT ON CONFLICT was introduced in Postgres 9.5; https://www.postgresql.org/docs/9.5/sql-insert.html . It also seems to be supported by the postgres backend in Django 2.2; https://github.com/django/django/commit/f1fbef6cd171ddfae41fcc901f1f60ccad039f51 .
Redshift is based on Postgres 8.0.2 and does not support INSERT ON CONFLICT. Therefore, INSERT ON CONFLICT must be disabled in some way.
I think your patch is a good solution because other backends that do not support ON CONFLICT simply return an empty string from ignore_conflicts_suffix_sql
.
The only other thing I would do is to set DatabaseFeatures.supports_ignore_conflicts = False
.
Since I don't have time to fix it now, I will prepare a UnitTest to reproduce the problem, fix it, and confirm that it works in the next few weeks. If you could prepare a PR for me, that would be great ;)
I can do that for sure!
I would like to include this request/PR on #167.
Subject: ProgrammingError when adding to ManyToManyField
Problem
Procedure to reproduce the problem
Same thing happens when trying to add from the admin site
Error logs / results
Expected results
New record(s) get added to the database
Environment info