Skip to content

Tracking model with FK to itself.  #312

Closed
@SergeyKubrak

Description

@SergeyKubrak

I have an issue when add history to the model which looks like:

class SomeModel(models.Model):
   uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
   parent = models.ForeignKey('self')

History model was created, and it has field parent but has wrong type, integer instead of UUID. And I've had and exceptions because of that.

I did fast review and we can fix it by improving CustomHistoricalRecords#copy_fields. We should use django.db.models.fields.related.resolve_relation, and update code in next way::

def copy_fields(self, model):
    .
    .
    .
    field = FieldType(
                    resolve_relation(model, old_field.rel.to),
                    related_name='+',
                    null=True,
                    blank=True,
                    primary_key=False,
                    db_index=True,
                    serialize=True,
                    unique=False,
                    on_delete=models.DO_NOTHING,
                    **field_arguments
                )
    .
    .
    .

It helps me. I use django 1.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedIssue accepted for completionbugIssues related to confirmed bugs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions