简体   繁体   English

与peewee和python的外键关系

[英]Foreign key relationship with peewee and python

I'm trying to set up a database ORM with peewee and am not clear on the use of foreign key relationships. 我正在尝试使用peewee建立数据库ORM,并且不清楚使用外键关系。

from peewee import *

db = SqliteDatabase('datab.db')

class datab(Model):
    class Meta:
        database = db

class Collection(datab):
    identifier = CharField()
    title = CharField()

class File(datab):
    identifier = ForeignKeyField(Collection, related_name='files')
    name = CharField()

Later, I do an import of "Collections" 后来,我导入了“收藏”

for value in collection:
Collection(**value).save()

Finally, where I am having trouble is adding the Files to the collections 最后,我遇到麻烦的地方是将文件添加到集合中

for value in collectionFiles:
    File(**value).save()

Within the value dict, there is a keyword pair with key of "identifier" and a value that should associate with the Collection identifier keyword. 在值dict中,有一个带有“identifier”键的关键字对和一个应该与Collection标识符关键字相关联的值。

However I get an error message: 但是我收到一条错误消息:

ValueError: invalid literal for int() with base 10: 'somevalue'

If I change the File(datab): identifier Type to VarChar, it saves the data. 如果我将File(datab):identifier Type更改为VarChar,它将保存数据。

I'm realizing I'm doing it wrong. 我意识到我做错了。 My assumption was that the unique identifier value in each table would apply the foreign key. 我的假设是每个表中的唯一标识符值将应用外键。 After reading the documentation, it looks like the foreign key setup is a bit different. 阅读文档后,外观设置看起来有点不同。 Do I need to do something like 我需要做些什么吗?

Collections.File.files(**values).save() ? Collections.File.files(** values).save()? In other words, instead of doing a data import, loading the collection object and then adding the file associated fields through peewee? 换句话说,不是进行数据导入,而是加载集合对象,然后通过peewee添加文件关联字段?

Values that make up class File 组成类File的值

{'crc32': '63bee49d',
 'format': 'Metadata',
 'identifier': u'somevalue',
 'md5': '34104ffce9e4084fd3641d0decad910a',
 'mtime': '1368328224',
 'name': 'lupi.jpg_meta.txt',
 'sha1': '1448ed1159a5d770da76067dd1c53e94d5a01535',
 'size': '1244'}

I think the naming of your fields might be part of the confusion. 我认为你的领域的命名可能是混乱的一部分。 Rather than calling the foreign key from File -> Collection "identifier", you might call it "collection" instead. 您可以将其称为“集合”,而不是从文件 - >集合“标识符”中调用外键。

class File(datab):
  collection = ForeignKeyField(Collection, related_name='files')
  name = CharField()

Peewee prefers that, when setting the value of a Foreign Key, it be a model instance. Peewee更喜欢在设置外键的值时,它是一个模型实例。 For example, rather than doing: 例如,而不是做:

File.create(collection='foobar', name='/secret/password')

It is preferable to do something like this: 最好做这样的事情:

collection = Collection.get(Collection.identifier == 'foobar')
File.create(collection=collection, name='/secret/password')

As a final note, if the Collection "identifier" is the unique primary key, you can set it up thus: 最后要注意,如果Collection“identifier”是唯一的主键,则可以这样设置:

class Collection(datab):
  identifier = CharField(primary_key=True)
  title = CharField()

(I'm not familiar with peewee, but if it's like Django then this should work.) (我不熟悉peewee,但如果它像Django那么这应该有效。)

class File has a ForeignKeyField and a CharField , so you can't simply save a pair of strings with File(**value) . class File有一个ForeignKeyField和一个CharField ,所以你不能简单地用File(**value)保存一对字符串。 You need to convert a string to a key first, like this: 您需要先将字符串转换为键,如下所示:

for value in collectionFiles:
    identifier = value['identifier']
    name = value['name']
    collection_entity = Collection.objects.filter(identifier=identifier).get()
    File(identifier=collection_entity, name=name).save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM