I'm having difficulty updating these three models which are linked with Foreign keys Reason they are linked with foreign keys is Events can have multiple Markets and Markets can have multiple Runners.
I'm at my wits end with this not null error. Even If I have a field that causes the issue and I remove the field from my model. Make migrations, migrate and remove the save from my task I still get the exact same error. What is the purpose of this not null argument? Even If I have null=True or null=False on brand new models I still get the error. Not null constraint failed
django.db.utils.IntegrityError: NOT NULL constraint failed: testapp_ma.event_id
I've no idea why this is failing.
Do I need to make sure all fields have a null argument? According to django documentation default is false. For each object my task runs all fields have data. So default of false should be fine. Could this be due to the manner I'm updating the models with my task?
full stacktrace here https://gist.github.com/Cally99/08fed46ba8039fa65d00f53e8a31b37a
class Event(models.Model):
sport_name = models.CharField(max_length=15)
event_id = models.BigIntegerField(unique=True, null=False)
event_name = models.CharField(max_length=200)
start_time = models.DateTimeField()
status = models.CharField(max_length=13)
class Market(models.Model):
event = models.ForeignKey(Event, on_delete=models.CASCADE)
market_id = models.BigIntegerField(unique=True)
market_name = models.CharField(max_length=35)
status = models.CharField(max_length=10)
volume = models.FloatField(null=True)
class Runner(models.Model):
market = models.ForeignKey(Market, null=True, default=None, on_delete=models.SET_NULL)
runner_id = models.BigIntegerField(unique=True)
event_id = models.BigIntegerField(null=True, default=0)
name = models.CharField(max_length=100)
tasks.py
@shared_task(bind=True)
def get_events(self):
api = get_client()
events = api.market_data.get_events(sport_ids=[9],states=MarketStates.All,
per_page=200, offset=0,
include_event_participants=Boolean.T,
category_ids=None, price_depth=3,
side=Side.All, session=None)
for event in events:
event_name = event["name"]
event_id = event['id']
start_time = event['start']
status = event["status"]
ev, created = Ev.objects.update_or_create(event_id=event_id)
ev.event_name = event_name
ev.start_time = start_time
ev.status = status
ev.save()
markets = event["markets"]
for market in markets:
event_id = market['event-id']
market_id = market['id']
market_name = market['name']
status = market['status']
volume = market['volume']
ma, created = Ma.objects.update_or_create(market_id=market_id)
ma.market_name = market_name
ma.status = status
ma.volume = volume
ma.save()
runners = market["runners"]
for runner in runners:
name = runner['name']
runner_id = runner['id']
event_id = runner['event-id']
runner, created = Ru.objects.update_or_create(runner_id=runner_id)
runner.event_id = event_id
runner.name = name
runner.save()
With the line
Ma.objects.update_or_create(market_id=market_id)
you haven't set the Event
for your market object. And given how
event = models.ForeignKey(Event, on_delete=models.CASCADE)
doesn't have null=True
, it won't accept any null values.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.