已被另一个外键使用的相关名称

时间:2016-11-08 08:24:03

标签: 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()
    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.

我没有运气改变了related_by值。 事实上我在controller.py中有一个Sub Class ModSubCat会导致问题吗?

我已删除related_name='group'

top_category = ForeignKeyField(Category, related_name='group')现在一切正常。

相关的名称是什么?

0 个答案:

没有答案
相关问题