Issue: Class with multiple foreign key references to the same table throws error
Author: chrismatta
State: closed
Created: 2013-09-11 19:46:52

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.


This is happening in 2.1.4, I just tested it in 2.1.0 and it seems to be working fine.


Do you have a foreign key on `InvArray` named `invhostdr_set`?


No, I don't. I only have one foreign key on InvArray that points to a different table, InvLocation.


Try setting the `related_name` attribute on either `primary_array` or `secondary_array`.


Still the same error, does this look right?

```
class InvHostDr(BaseModel):
    comment = TextField()
    primary_array = ForeignKeyField(
        db_column='primary_array_id', related_name="primary", 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', related_name="secondary", 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'
```


You will want to add related name attributes for all `secondary_host` and `secondary_project_host` -- any time you have multiple foreign keys to the same table you will need to specify a related name to avoid collisions.


What is colliding here? Is it the field in InvZoneSet colliding with it's own related name? I'm not sure why there needs to be a different name, 

```
class InvZoneSet(BaseModel):
    fabric = ForeignKeyField(db_column='fabric_id', rel_model=InvFabric)
    name = CharField()
    sibling = IntegerField(db_column='sibling_id')
    vsan = ForeignKeyField(db_column='vsan_id', related_name="vsan", rel_model=InvFabric)

    class Meta:
        db_table = 'inv_zone_set'
```

error:

```
AttributeError: Foreign key: invzoneset.vsan related name "vsan" collision with field of the same name.
```


Does `InvFabric` have a field name `vsan`?


The field name in the database for InvZoneSet is is `vsan_id`, the field name for InvFabric is `vsan` is that still considered a collision?


Sorry, my mistake, they're both `vsan_id`. What exactly is colliding if there are two references to the same table in one class? They have different field names, and it will be likely that the referenced table will have the same field name as the class that's referencing it, no?


The fact that they are accessed in python using the same identifier (vsan).


Ok, I resolved this, thanks.


Charles,

I'm facing the same problem.

Here a simple example to reproduce:

``` python
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.