question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttributeError with multiple ForeignKeyField to the same table.

See original GitHub issue

Charles,

Here a simple example to reproduce:

from peewee import *

class Parent(Model):
    name = CharField()

class Student(Model):
    name = CharField()
    father = ForeignKeyField(Parent)
    mother = ForeignKeyField(Parent)
AttributeError: Foreign key: student.mother related name "student_set" collision with foreign key using same related_name.

It seems that peewee tries to make a set with all the references to same table, but there is case where the attributes are different.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
coleifercommented, Oct 2, 2013

This is because there are two foreign keys to the same model on Student. Specify related_name attributes (the backref on Parent):

class Student(Model):
    name = CharField()
    father = ForeignKeyField(Parent, related_name='father_students')
    mother = ForeignKeyField(Parent, related_name='mother_students')

This is a new error that is raised because both parent fks would use the same backref (student_set), which is ambiguous.

1reaction
coleifercommented, Jan 22, 2015

I’ll look into it, but I’m going to go out on a limb and say that pwiz does a pretty damn good job at what it does.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sqlalchemy error, multiple foreign keys references to the same ...
This works for me in version 1.4: class Message(Base): __tablename__ = "messages" id = sa.Column(sa.Integer, primary_key=True) sender_id ...
Read more >
The right way to use a ManyToManyField in Django
To maintain a many-to-many relationship between two tables in a database, the only way is to have a third table which has references...
Read more >
Describing Databases with MetaData
MetaData is a container object that keeps together many different features of a database (or multiple databases) being described.
Read more >
peewee - Models and Fields - Documentation sets
ForeignKeyField is a special field type that allows one model to reference another. · Foreign keys allow data to be normalized. · If...
Read more >
Making queries | Django documentation
To represent database-table data in Python objects, Django uses an ... Updating a ForeignKey field works exactly the same way as saving a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found