Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreign key relationship with peewee and python

Tags:

python

orm

peewee

I'm trying to set up a database ORM with peewee and am not clear on the use of foreign key relationships.

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.

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.

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() ? In other words, instead of doing a data import, loading the collection object and then adding the file associated fields through peewee?

Values that make up class File

{'crc32': '63bee49d',
 'format': 'Metadata',
 'identifier': u'somevalue',
 'md5': '34104ffce9e4084fd3641d0decad910a',
 'mtime': '1368328224',
 'name': 'lupi.jpg_meta.txt',
 'sha1': '1448ed1159a5d770da76067dd1c53e94d5a01535',
 'size': '1244'}
like image 489
Justin Avatar asked Jun 11 '13 02:06

Justin


People also ask

What is peewee in Python?

Peewee is a Python ORM (Object-Relational Mapping) library which supports SQLite, MySQL, PostgreSQL and Cockroach databases.

What is backref in peewee?

Peewee uses ForeignKeyField to define foreign-key relationships between models. Every foreign-key field has an implied back-reference, which is exposed as a pre-filtered Select query using the provided backref attribute.


1 Answers

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. 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:

class Collection(datab):
  identifier = CharField(primary_key=True)
  title = CharField()
like image 74
coleifer Avatar answered Sep 16 '22 22:09

coleifer