Skip to content

Django 1.4 enabling compositekey breaks unrelated models #35

@anentropic

Description

@anentropic

I wanted to use compositekey in the following model:

from compositekey import db

class Relationship(models.Model):
    class Meta:
        db_table = 'follows'

    id = db.MultiFieldPK("u", "tu")
    u = models.IntegerField(db_column='user_id')
    tu = models.IntegerField(db_column='target_user_id')
    d = models.DateTimeField(db_column='created', default=datetime.utcnow)

It's a new model and none of my other models have any fk relation to it.

However when I add this I get an exception when I try a manage.py shell etc

  File "/Users/anentropic/Documents/Dev/Work/myproj/otherapp/models.py", line 57, in <module>
    class UnrelatedModel(AnotherUnrelatedModel):
  File "/Users/anentropic/.virtualenvs/myproj/lib/python2.7/site-packages/django/db/models/base.py", line 167, in __new__
    new_class.add_to_class(field.name, copy.deepcopy(field))
  File "/Users/anentropic/.virtualenvs/myproj/lib/python2.7/site-packages/django/db/models/base.py", line 219, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/anentropic/.virtualenvs/myproj/lib/python2.7/site-packages/compositekey/db/models/fields/related.py", line 31, in contribute_to_class
    assert not self.fields_ext or not self._fields,\
AttributeError: 'ForeignKey' object has no attribute 'fields_ext'

As best I can tell it looks like maybe the fk monkeypatch has arrived too late to affect models which have already been imported by the time I reach my first model using a composite key?

if so it's odd that this isn't mentioned in the docs?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions