Reputation: 2253
I've been trying to reset my database and figure out why this is happening, but for some reason I'm getting this error:
IntegrityError: main_funding_rounds_investments.investment_id may not be NULL
I can't figure out how or why the automatic id field would be null? If anyone has ideas I would greatly appreciate it!
When i check the db with inspectdb, I get the following:
class MainFundingRoundsInvestments(models.Model):
id = models.IntegerField(primary_key=True)
funding_rounds_id = models.IntegerField()
investment_id = models.IntegerField()
class Meta:
db_table = u'main_funding_rounds_investments'
and
class MainInvestment(models.Model):
id = models.IntegerField(primary_key=True)
class Meta:
db_table = u'main_investment'
Here are my models:
#funding rounds
class funding_rounds(models.Model):
funded_day = models.IntegerField(null=True)
investments = models.ManyToManyField("Investment")
class Investment(models.Model):
company = models.ManyToManyField("Company", null=True, related_name ="Investments_company")
financial_org = models.ManyToManyField("Financial_org", null=True, related_name ="Investments_financial_org")
person = models.ManyToManyField("Person", null=True, related_name ="Investments_person")
This is where I create the objects:
def save_dict_to_db_R(model_class ,dicta):
testCo = model_class()
for key,value in dicta.items():
try:
field = model_class._meta.get_field(key)
if isinstance(field, models.ManyToManyField):
continue
else:
if model_class._meta.get_field(key):
print("i == something:" + key + " ")
setattr(testCo, key, value)
except FieldDoesNotExist:
continue
testCo.save(
for field in model_class._meta.many_to_many:
if field.name in dicta and hasattr(dicta[field.name], 'append'):
for obj in dicta[field.name]:
rel_instance = save_dict_to_db_R(field.rel.to, obj)
getattr(testCo, field.name).add(rel_instance)
Upvotes: 0
Views: 1671
Reputation: 599778
Why have you set the primary key field manually? Unless you need to call the field something different from id
, or give it different attributes, you should just leave it out of the model and let Django add it automatically.
Your immediate problem is that you've used IntegerField instead of AutoField, but if you removed your definition automatically that would be done for you.
Upvotes: 1