Peewee: Foreign keys must specify a `rel_field`

Created on 6 Apr 2018  路  2Comments  路  Source: coleifer/peewee

Hi Charles,

Thank you for your excellent library 馃檶

I'm trying to do some migrations by adding a ForeignKeyField, but I'm getting this error:
https://github.com/coleifer/peewee/blob/master/playhouse/migrate.py#L264

I couldn't find any documentation on what the rel_field is supposed to be.

Code

with my_db.transaction():
    migrate.migrate(
        migrator.add_column('service', 'parent', peewee.ForeignKeyField(
            db.ServiceCategory, null=True, on_delete='SET NULL'))
    )
ValueError: Foreign keys must specify a `rel_field`.

After adding a rel_field

with my_db.transaction():
    migrate.migrate(
        migrator.add_column('service', 'parent', peewee.ForeignKeyField(
            db.ServiceCategory, rel_field='servicecategory', null=True, on_delete='SET NULL'))
    )
Traceback (most recent call last):
  File "run.py", line 179, in <module>
    db.migrations.run()
  File "/Users/csytan/Documents/dev/cozycal/db/migrations/__init__.py", line 18, in run
    importlib.import_module('db.migrations.{}'.format(version))
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/csytan/Documents/dev/cozycal/db/migrations/51.py", line 11, in <module>
    db.ServiceCategory, rel_field='servicecategory', null=True, on_delete='SET NULL'))
  File "/Users/csytan/Library/Python/3.6/lib/python/site-packages/peewee.py", line 4127, in __init__
    super(ForeignKeyField, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'rel_field'

Most helpful comment

# e.g., to reference the "id" field of ServiceCategory:
ForeignKeyField(db.ServiceCategory, db.ServiceCategory.id, null=True, on_delete='SET NULL')

All 2 comments

The error message is out-of-date. The parameter name is "field" (no "rel_"). It refers to the field on the model your FK points to - typically the primary key.

# e.g., to reference the "id" field of ServiceCategory:
ForeignKeyField(db.ServiceCategory, db.ServiceCategory.id, null=True, on_delete='SET NULL')
Was this page helpful?
0 / 5 - 0 ratings

Related issues

kadnan picture kadnan  路  3Comments

rayzorben picture rayzorben  路  4Comments

ezk84 picture ezk84  路  4Comments

razodactyl picture razodactyl  路  3Comments

ghost picture ghost  路  5Comments