Repository: jazzband/django-simple-history

Issue:
IntegrityError: Using Historical Records on an object that inherits from another non abstract object
This is just as of the latest version. I cannot edit any objects that inherit from another object.
If I create two objects as so:

```
class Zork(models.Model):
    data = models.IntegerField(blank=True, null=True, default=0)


class Bork(Zork):
    different_data = models.IntegerField(blank=True, null=True, default=0)
    history = HistoricalRecords()
```

Steps to repro:
1. Create an object of type Bork (this creates the object as well as a simplehistory record for bork)
2. Try to edit the object (this will try to create another historical record but will fail with an integrity error)

Reason is the historical record object has a OneToOne relationship to Zork, so when we try to create another historical record for zork, it throws an Integrity Error



Discussion:
This is with Django 1.8? I put your case almost verbatim into a test method, and it's not reproducing it. It might be a problem with SQLite not reproducing the correct constraints.

Yes, this occurred on Django 1.8. That's strange, I was using SQLite for my test too. I'll try to find more details to repro.

Ok I've updated my original comment... In my test case, I also had "different_data" in Bork. But I'm not sure how it should make a difference.  Perhaps this will help.. I see this in my migrations (last line has the one to one field that's causing the issue):

```
        migrations.CreateModel(
            name='HistoricalZork',
            fields=[
                ('id', models.IntegerField(verbose_name='ID', db_index=True, auto_created=True, blank=True)),
                ('data', models.IntegerField(default=0, null=True, blank=True)),
                ('different_data', models.IntegerField(default=0, null=True, blank=True)),
                ('history_id', models.AutoField(serialize=False, primary_key=True)),
                ('history_date', models.DateTimeField()),
                ('history_type', models.CharField(max_length=1, choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')])),
                ('history_user', models.ForeignKey(related_name='+', on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL, null=True)),
                ('zork_ptr', models.OneToOneField(related_name='+', null=True, db_constraint=False, blank=True, to='blog.Zork')),
            ],
```


Resolving patch:
diff --git a/simple_history/models.py b/simple_history/models.py
--- a/simple_history/models.py
+++ b/simple_history/models.py
@@ -4,6 +4,7 @@
 import copy
 import warnings
 
+import django
 from django.db import models, router
 from django.db.models import loading
 from django.db.models.fields.proxy import OrderWrt
@@ -129,18 +130,26 @@ def copy_fields(self, model):
                 field.__class__ = models.IntegerField
             if isinstance(field, models.ForeignKey):
                 old_field = field
-                field = type(field)(
-                    field.rel.to,
+                field_arguments = {}
+                if (getattr(old_field, 'one_to_one', False) or
+                        isinstance(old_field, models.OneToOneField)):
+                    FieldType = models.ForeignKey
+                else:
+                    FieldType = type(old_field)
+                if django.get_version() >= "1.6":
+                    field_arguments['db_constraint'] = False
+                field = FieldType(
+                    old_field.rel.to,
                     related_name='+',
                     null=True,
                     blank=True,
                     primary_key=False,
                     db_index=True,
                     serialize=True,
+                    unique=False,
+                    **field_arguments
                 )
-                field._unique = False
                 field.name = old_field.name
-                field.db_constraint = False
             else:
                 transform_field(field)
             fields[field.name] = field


Test patch: