简体   繁体   English

不是 null 约束失败。 Django 模型-Postgres 相同的错误 null=True 与 null=False

[英]Not null constraint failed. Django Models-Postgres Same error null=True as null=False

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.这不是 null 错误,我束手无策。 Even If I have a field that causes the issue and I remove the field from my model.即使我有一个导致问题的字段并且我从我的 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?这个不是 null 论点的目的是什么? Even If I have null=True or null=False on brand new models I still get the error.即使我在全新型号上使用 null=True 或 null=False,我仍然会收到错误消息。 Not null constraint failed不是 null 约束失败

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?我是否需要确保所有字段都有 null 参数? According to django documentation default is false.根据 django 文档默认为 false。 For each object my task runs all fields have data.对于每个 object 我的任务运行,所有字段都有数据。 So default of false should be fine.所以默认 false 应该没问题。 Could this be due to the manner I'm updating the models with my task?这可能是由于我使用任务更新模型的方式吗?

full stacktrace here https://gist.github.com/Cally99/08fed46ba8039fa65d00f53e8a31b37a完整的堆栈跟踪 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任务.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.您尚未为您的市场 object 设置Event And given how鉴于如何

event = models.ForeignKey(Event, on_delete=models.CASCADE)

doesn't have null=True , it won't accept any null values.没有null=True ,它不会接受任何 null 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM