Python 其他外键已在使用相关名称
我使用peewee创建了一个小的sqlite3数据库,试图理解外键并使它们在一个简单的数据库中工作Python 其他外键已在使用相关名称,python,sqlite,peewee,Python,Sqlite,Peewee,我使用peewee创建了一个小的sqlite3数据库,试图理解外键并使它们在一个简单的数据库中工作 from peewee import * db = SqliteDatabase('database.db') class Category(Model): category = CharField() class Meta: database = db class Subcat(Model): description = BlobField()
from peewee import *
db = SqliteDatabase('database.db')
class Category(Model):
category = CharField()
class Meta:
database = db
class Subcat(Model):
description = BlobField()
sub_cat = CharField()
top_category = ForeignKeyField(Category, related_name='group')
class Meta:
database = db
db.connect()
db.create_tables([Category, Subcat], safe=True)
然后,我创建了一个controller.py文件来处理所有数据库事务。
(更新)
从peewee进口*
从数据库导入*
class ModCat(Category):
def add_cat(self,category):
update = Category.create(category=category)
def get_cat(self):
categories = Category.select(Category.category).order_by(Category.category)
return categories
class ModSubCat(Subcat):
def save_sub_cat(self, sub, master, desc):
name = Category().select().where(Category.name==master)
update = Subcat.create(sub_cat=sub, top_category=name, description=desc)
最后是一个main.py,它允许我使用在wxFormBuilder中创建的简单表单将数据输入数据库
from gui import *
from controller import *
class Menu(InDb):
def __init__(self, parent):
InDb.__init__(self, parent)
get_categories = ModCat()
list = get_categories.get_cat()
new_list = []
for thing in list:
new_list.append(thing.category)
print new_list
for f in new_list:
self.m_comboBox1.Append(f)
def click_save( self, event ):
new_cat = ModCat()
new_cat.add_cat(self.m_textCtrl3.GetValue())
self.GetParent() # This assigns parent frame to frame.
self.Close() # This then closes frame removing the main menu.
frame = Menu(None)
frame.Centre()
frame.Show()
def sub_save( self, event ):
sub = self.m_textCtrl5.GetValue()
master = self.m_comboBox1.GetValue()
desc = "Hi"
update = ModSubCat()
update.save_sub_cat(sub, master, desc)
#Start App by calling sub class of MainMenu
if __name__ == '__main__':
app = wx.App(0)
frame = Menu(None)
frame.Centre()
frame.Show()
app.MainLoop()
数据库创建时没有错误,但当我运行main.py时,它总是返回此错误
Traceback (most recent call last):
File "C:/Users/********/PycharmProjects/RAMS/main.py", line 2, in <module>
from controller import *
File "C:\Users\********\PycharmProjects\RAMS\controller.py", line 13, in <module>
class ModSubCat(Subcat):
File "C:\Python27\lib\site-packages\peewee.py", line 4710, in __new__
field.add_to_class(cls, name)
File "C:\Python27\lib\site-packages\peewee.py", line 1437, in add_to_class
invalid('The related_name of %(field)s ("%(backref)s") '
File "C:\Python27\lib\site-packages\peewee.py", line 1431, in invalid
raise AttributeError(msg % context)
AttributeError: The related_name of modsubcat.top_category ("group") is already in use by another foreign key.
回溯(最近一次呼叫最后一次):
文件“C:/Users/********/PycharmProjects/RAMS/main.py”,第2行,在
从控制器导入*
文件“C:\Users\******\PycharmProjects\RAMS\controller.py”,第13行,在
类别ModSubCat(Subcat):
文件“C:\Python27\lib\site packages\peewee.py”,第4710行,在新的__
字段。将\添加到\类(cls,名称)
文件“C:\Python27\lib\site packages\peewee.py”,第1437行,在add\u to\u类中
无效((%field)s(“%(backref)s”)的相关_名称)
文件“C:\Python27\lib\site packages\peewee.py”,第1431行,格式无效
引发AttributeError(消息%context)
AttributeError:modsubcat.top类别(“组”)的相关名称已被其他外键使用。
我已经改变了你的价值没有运气。
我在controller.py中有一个子类ModSubCat这一事实是否导致了问题
我删除了相关的\u name='group'
从
top\u category=ForeignKeyField(category,related\u name='group')
现在所有的工作都完成了
什么是相关的名称?这里也有同样的问题。迁移工作正常,因此似乎不是完全重复的
相关的名称
,相关的名称
是关系的名称,就像在Django中一样。框架需要区分,通常只有在同两个表之间有多个关系时才需要。Sa这里有一个问题。迁移工作正常,因此似乎不完全是重复的related\u name
,related\u name
是关系的名称,就像在Django中一样。这是框架需要区分的,通常只有当它们是同两个表之间的多个关系时才需要。