Search code examples
djangodjango-modelsmany-to-manydjango-ormdjango-postgresql

why i cant save m2m relationships in Django 3


recently i have upgrade and app to django 3, and i'm using a many to many relationship to define the people i want to send and email, in this way:

class Contact(Master):
    name = models.CharField(max_length=100, blank=True, null=True, verbose_name=_('name'))
    email = models.EmailField(verbose_name=_("Email"), max_length=70, db_index=True, unique=True)

    class Meta:
        verbose_name = _("contact")
        verbose_name_plural = _("contacts")

    def __str__(self):
        return "{} - {}".format(self.name, self.email)

class Newsletter(Master):

    template = models.CharField(verbose_name="template",max_length=100,null=True,blank=True)
    subject = models.CharField(max_length=100,null=False)
    text_content = models.TextField(blank=True,null=True)
    send = models.BooleanField(db_index=True,default=True)
    contacts = models.ManyToManyField('contacts.Contact', blank=True, related_name="contacts", verbose_name=_('contacts'))

i make all the migrations and everything looks normal, but when i try to add contacts to the newsletter, via "admin" or "shell" it shows this error:

ProgrammingError at /es/admin/contacts/newsletter/4/change/

syntax error at or near "ON"
LINE 1: ... ("newsletter_id", "contact_id") VALUES (4, 1861) ON CONFLIC...

the complete traceback error shows this

Traceback (most recent call last):
  File "/home/beren5000/lib/python3.8/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.SyntaxError: syntax error at or near "ON"
LINE 1: ... ("newsletter_id", "contact_id") VALUES (4, 1861) ON CONFLIC...
                                                             ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/beren5000/lib/python3.8/django/db/models/fields/related_descriptors.py", line 944, in add
    self._add_items(
  File "/home/beren5000/lib/python3.8/django/db/models/fields/related_descriptors.py", line 1123, in _add_items
    self.through._default_manager.using(db).bulk_create([
  File "/home/beren5000/lib/python3.8/django/db/models/query.py", line 492, in bulk_create
    returned_columns = self._batched_insert(
  File "/home/beren5000/lib/python3.8/django/db/models/query.py", line 1230, in _batched_insert
    self._insert(item, fields=fields, using=self.db, ignore_conflicts=ignore_conflicts)
  File "/home/beren5000/lib/python3.8/django/db/models/query.py", line 1204, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/home/beren5000/lib/python3.8/django/db/models/sql/compiler.py", line 1384, in execute_sql
    cursor.execute(sql, params)
  File "/home/beren5000/lib/python3.8/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/home/beren5000/lib/python3.8/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/beren5000/lib/python3.8/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/beren5000/lib/python3.8/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "/home/beren5000/lib/python3.8/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/beren5000/lib/python3.8/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: syntax error at or near "ON"
LINE 1: ... ("newsletter_id", "contact_id") VALUES (4, 1861) ON CONFLIC...

it seems to be an error or something in the psycopg2, but i had never clash with an error like this, the version of psycopg2 is psycopg2==2.8.4

thanks for your help


Solution

  • django 3 drop support for postgres 9.4 https://docs.djangoproject.com/en/3.0/releases/3.0/#dropped-support-for-postgresql-9-4