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.

Class with multiple foreign key references to the same table throws error

See original GitHub issue

After upgrading this class throws an error:

class InvHostDr(BaseModel):
    comment = TextField()
    primary_array = ForeignKeyField(
        db_column='primary_array_id', rel_model=InvArray)
    primary_host = ForeignKeyField(
        db_column='primary_host_id', rel_model=InvHost)
    primary_project_host = ForeignKeyField(
        db_column='primary_project_host_id', rel_model=PrjProjectHost)
    primary_shadowimage = TextField()
    primary_volumes = TextField()
    replication_group = CharField()
    secondary_array = ForeignKeyField(
        db_column='secondary_array_id', rel_model=InvArray)
    secondary_host = ForeignKeyField(
        db_column='secondary_host_id', rel_model=InvHost)
    secondary_project_host = ForeignKeyField(
        db_column='secondary_project_host_id', rel_model=PrjProjectHost)
    secondary_shadowimage = TextField()
    secondary_volumes = TextField()

    class Meta:
        db_table = 'inv_host_dr'

Here’s the error:

AttributeError: Foreign key: invhostdr.primary_array related name "invhostdr_set" collision with foreign key using same related_name.

I’m not even calling this class in the code, it errors when the module is initialized.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
andmartcommented, Oct 1, 2013

Charles,

I’m facing the same problem.

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)

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.

0reactions
chrismattacommented, Sep 19, 2013

Ok, I resolved this, thanks.

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 >
How to Add Multiple Foreign Keys to Same Table and Not Get ...
Start by creating tables without any foreign keys. If you try to create tables with foreign keys that reference other tables that are...
Read more >
3 common foreign key mistakes (and how to avoid them)
1. Mismatched data types ... When you're using a foreign key to reference a column in another table, the datatypes of both tables...
Read more >
13.1.18.5 FOREIGN KEY Constraints - MySQL :: Developer Zone
A foreign key constraint is defined on the child table. ... Foreign Key Errors ... One foreign key references a two-column index in...
Read more >
Changing Foreign Keys and Navigations - EF Core
Relationships in an Entity Framework Core (EF Core) model are represented using foreign keys (FKs). An FK consists of one or more properties ......
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