【发布时间】:2017-01-24 13:31:48
【问题描述】:
我在一个带有 MySQL DB 的小 Python 脚本中使用 Peewee 作为 ORM。
#!/usr/bin/python3
#coding: utf-8
import peewee
from peewee import *
db = MySQLDatabase(**config)
class Foo(peewee.Model):
bar = peewee.CharField(unique=True, null=False)
class Meta:
database = db
try:
Foo.create_table()
except:
pass
foo_data = [{'bar':'xyz'},{'bar':'xyz'}]
Foo.insert_many(foo_data).on_conflict(action='IGNORE').execute()
如你所见,我有同样的钥匙。我想第二次使用on_conflict 方法忽略它(described in the API reference,但仅适用于 SQLite3),但运行脚本时出现此错误(正常,因为未针对 MySQL 实现):
peewee.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OR IGNORE INTO `foo` (`bar`) VA' at line 1")
如果我删除.on_conflict(action='IGNORE'),MySQL 也不喜欢它(重复键)。如果它是重复键,我如何让 peewee 插入新键或忽略它?
【问题讨论】:
标签: python mysql python-3.x orm peewee