我正在从网页中提取数据。 “块”表示给定页面上的文本块或图像块。我提取了该块的选择器列表。选择器(对象)具有类列表(SelecClass对象)。一个Selector对象可能具有许多SelecClass对象,而SelecClass对象可能存在于其他Selector中。因此,我将Selector和SelecClass对象之间的关系存储为通过表的多对多关系。
但是,当我有一个重复的SelecClass值时,我得到:
peewee.IntegrityError: UNIQUE constraint failed: selecclass_selector_through.selecclass_id, selecclass_selector_through.selector_id
我不明白为什么。由于这是一张贯通表,我希望会有重复的Selector ID和SelecClass ID。我误会了吗?
如果是,该如何解决?我尝试在ManyToManyField上设置“ unique = False”,但无法识别该参数。
如果您安装了peewee,则可以在本地运行以下代码以复制错误。
from peewee import *
if __name__ == '__main__':
# DATABASE SETUP
database = SqliteDatabase('test_db.db')
class BaseModel(Model):
class Meta:
database = database
class Block(BaseModel):
text = TextField(null=True)
class SelectorID(BaseModel):
name = CharField(unique=True)
class SelectorName(BaseModel):
name = CharField(unique=True)
class Selector(BaseModel):
block = ForeignKeyField(Block, backref='selectors')
selector_id = ForeignKeyField(SelectorID, backref='selectors')
name = ForeignKeyField(SelectorName, backref='selectors')
class SelecClass(BaseModel):
selectors = ManyToManyField(Selector, backref='selector_classes')
name = CharField(unique=True)
def create_tables():
database.connect()
with database:
database.create_tables([Block,
SelectorID, SelectorName, Selector,
SelecClass,
SelecClass.selectors.get_through_model()])
database.close()
create_tables()
# DATA
block = {'text': 'test'}
selectors = [
{
"classes": [
"wrapper"
],
"id": "",
"name": "div"
},
{
"classes": [
"row"
],
"id": "",
"name": "div"
},
{
"classes": [
"row" # When I try to insert this selector's class_obj, a duplicate, I get an IntegrityError
],
"id": "",
"name": "div"
},
]
# DATA INSERT
block_object = Block.create(text=block['text'])
selectors_and_classes = []
with database.atomic():
for selector in selectors:
selector_id, _ = SelectorID.get_or_create(name=selector['id'])
selector_name, _ = SelectorName.get_or_create(name=selector['name'])
selector_obj = Selector.create(
block=block_object,
selector_id=selector_id,
name=selector_name,
)
s2bc_class_objs = []
for s2bc_class in selector['classes']:
selector_class_obj, _ = SelecClass.get_or_create(name=s2bc_class)
s2bc_class_objs.append(selector_class_obj)
selectors_and_classes.append((selector_obj, s2bc_class_objs))
with database.atomic():
for selector_and_classes in selectors_and_classes:
selector = selector_and_classes[0]
selector_class_objs = selector_and_classes[1]
Selector = Selector.get(selector_obj)
for class_obj in selector_class_objs:
Selector.selector_classes.add(class_obj)
跟踪:
Traceback (most recent call last):
File "C:\h\envs\mlpc\lib\site-packages\peewee.py", line 2936, in execute_sql
cursor.execute(sql, params or ())
sqlite3.IntegrityError: UNIQUE constraint failed: selecclass_selector_through.selecclass_id, selecclass_selector_through.selector_id
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 108, in <module>
Selector.selector_classes.add(class_obj)
File "C:\h\envs\mlpc\lib\site-packages\peewee.py", line 6810, in add
accessor.through_model.insert_many(inserts).execute()
File "C:\h\envs\mlpc\lib\site-packages\peewee.py", line 1778, in inner
return method(self, database, *args, **kwargs)
File "C:\h\envs\mlpc\lib\site-packages\peewee.py", line 1849, in execute
return self._execute(database)
File "C:\h\envs\mlpc\lib\site-packages\peewee.py", line 2567, in _execute
return super(Insert, self)._execute(database)
File "C:\h\envs\mlpc\lib\site-packages\peewee.py", line 2316, in _execute
cursor = database.execute(self)
File "C:\h\envs\mlpc\lib\site-packages\peewee.py", line 2949, in execute
return self.execute_sql(sql, params, commit=commit)
File "C:\h\envs\mlpc\lib\site-packages\peewee.py", line 2943, in execute_sql
self.commit()
File "C:\h\envs\mlpc\lib\site-packages\peewee.py", line 2725, in __exit__
reraise(new_type, new_type(*exc_args), traceback)
File "C:\h\envs\mlpc\lib\site-packages\peewee.py", line 183, in reraise
raise value.with_traceback(tb)
File "C:\h\envs\mlpc\lib\site-packages\peewee.py", line 2936, in execute_sql
cursor.execute(sql, params or ())
peewee.IntegrityError: UNIQUE constraint failed: selecclass_selector_through.selecclass_id, selecclass_selector_through.selector_id
我知道错误的含义,我只是不明白为什么要得到它(在我认为可以接受重复的贯通表上),或者如何更改它。