I have implemented some models and migrations into my postgreSQL9.4 database works. I have some models. For an example the SkillLevelRuleModel:
class SkillLevelRule(models.Model):
skillLevelRuleID = models.AutoField(primary_key=True)
skillCategoryID = models.ForeignKey(SkillCategory, default=1)
skillLevelID = models.ForeignKey(SkillLevel, default=1)
threshold = models.IntegerField()
validFrom = models.DateTimeField(default=datetime.now)
validTo = models.DateTimeField(auto_now_add=False, auto_now=True)
Now I have a problem with the following field:
validTo = models.DateTimeField(auto_now_add=False, auto_now=True)
So it should have no value in the beginning when the model is created, but as soon as an entry in the model is updated, validTO should be updated.
As soon as I have created the model (makemigrations, migrate) I want to load an initial_data.json to load initial data.
Sadly I get the following error:
Could not load rewardsystem.SkillLevelRule(pk=6): null value in column "validTo" violates not-null constraint
DETAIL: Failing row contains (6, 2, 2016-02-11 08:46:29.267201+00, null, 1, 1).
My .json file and the respective entry for this models looks like the following:
{
"model": "rewardsystem.SkillLevelRule",
"pk": 6,
"fields":{
"skillCategoryID" : "1",
"skillLevelID" : "1",
"threshold" : "2"
}
},
I do not understand why this happens and how I should prevent it. I do not want that the validTo field is set in the beginning. Why? This model saves rules or let's say thresholds. They are valid from the beginning, when the model is instantiated. Maybe at some point when the system is running, an administrator likes to change some rules. Therefore he should be able to specify until when a certain rule WAS VALID (validTO).