I'm trying a simple model inheritance:
class Product(models.Model):
creation_date = models.DateTimeField(auto_now_add=True)
class Application(Product):
name = models.CharField(max_length=200)
'makemigrations' asks for a default:
You are trying to add a non-nullable field 'product_ptr' to application without a default; we can't do that (the database needs something to populate existing rows).
I saw here that I could Product an abstract model with a Meta class, but I can't do that since I'm refereing it specifically in other models as an actual model:
class Comment(models.Model):
product = models.ForeignKey('Product', related_name="comments")
Running 'makemigrations' when the database is removed also leads to the same issue.
Anything I can do?
Django 1.9
You haven't explained what exactly is the change that you have made, it seems that you have changed your Application
model to inherit from Product
where it previously inherited from models.Model. That causes django to create a 1 to 1 mapping behind the scenes. The addition of the product_ptr which you didn't add to the model yourself enters the picture
ref: https://docs.djangoproject.com/en/1.9/topics/db/models/#multi-table-inheritance
The inheritance relationship introduces links between the child model and each of its parents (via an automatically-created OneToOneField).
It's a bit tricky to add this field to a table with data during a migration because this field needs to be unique. If you are merely creatig a new model named Application
, a value of 1 would be ok.