【问题标题】:psycopg2.errors.SyntaxError: syntax error at or near "ON"psycopg2.errors.SyntaxError:在“ON”处或附近出现语法错误
【发布时间】:2020-08-29 15:39:47
【问题描述】:

我正在尝试 Django 文档中的 Django。 我在我的投票应用程序的 models.py 中创建了两个类:PublicationArticles,具有多对多字段:

class Article(models.Model):
    publications=models.ManyToMany(Publication, ...)

然后在python shell中,我导入了ran:

>>> from polls.models import Publication,Articles
>>> p1=Publication.objects.get(id=1)
>>> p2=Publication.objects.get(id=2)
>>> p3=Publication.objects.get(id=3)
>>> a1=Articles(headlines="When will be the lockdown get finished?")
>>> a1.publications.add(p1) 

我收到一个错误:

Traceback (most recent call last):
  File "C:\Python38\lib\site-packages\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: ..." ("articles_id", "publication_id") VALUES (2, 1) ON CONFLIC...

【问题讨论】:

  • 请分享查询
  • @Gabip OP 没有直接运行查询,它是由a1.publications.add(p1) 行生成的。

标签: python django django-models many-to-many django-3.0


【解决方案1】:

Django 3.0 supports PostgreSQL 9.5 and higher。您收到该错误是因为您使用的是不支持 ON CONFLICT 的早期版本的 PostgreSQL。

要解决此问题,您可以升级 PostgreSQL,或降级到支持 PostgreSQL 9.4 及更高版本的最新 Django 2.2.X。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 2020-08-20
    • 2021-12-13
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多