【问题标题】:You are trying to add a non-nullable field 'id' to a model您正在尝试向模型添加不可为空的字段“id”
【发布时间】:2016-01-18 01:59:24
【问题描述】:

我正在尝试开发一个网络应用程序,该应用程序提供在 django 上创建迷你游戏联盟。在创建数据库的那一刻,我收到以下错误:

您正在尝试在没有默认值的情况下向 juego 添加不可为空的字段“id”;我们不能这样做(数据库需要一些东西来填充现有的行)。

这是我在文件models.py 上的课程Juego

class Juego(models.Model):
   nombre_juego = models.CharField(max_length=100)
   record = models.DecimalField(max_digits=10000000, decimal_places=3)
   fecha_inicio = models.DateTimeField(default=timezone.now)
   fecha_fin = models.DateTimeField(default=timezone.now)
   enlace = models.CharField(max_length=1000)

在上次迁移后,我在模型中添加了以下类:

class Juegan(models.Model):
    user = models.ManyToManyField(User)
    nombre_juego = models.ManyToManyField(Juego)
    puntuacion = models.DecimalField(max_digits=10000000, decimal_places=3)

【问题讨论】:

  • 自巡演上次迁移以来,您还做了什么?
  • 我将以下类添加到模型中:
  • 请使用编辑链接在您的问题中提供更多详细信息,不要在 cmets 中添加它们!
  • 您之前是否在其他字段中使用过primary_key=True
  • 我看不出问题出在哪里。如果数据库中没有重要数据,您可能会发现最简单的方法是重新创建数据库,删除应用中的迁移,然后重新创建迁移。

标签: python django django-models


【解决方案1】:

您现在的代码没有任何问题。我刚刚做了一个 django 项目并运行了它:

型号:

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User

# Create your models here.
class Juego(models.Model):
   nombre_juego = models.CharField(max_length=100)
   record = models.DecimalField(max_digits=10000000, decimal_places=3)
   fecha_inicio = models.DateTimeField(default=timezone.now)
   fecha_fin = models.DateTimeField(default=timezone.now)
   enlace = models.CharField(max_length=1000)

class Juegan(models.Model):
    user = models.ManyToManyField(User)
    nombre_juego = models.ManyToManyField(Juego)
    puntuacion = models.DecimalField(max_digits=10000000, decimal_places=3)

迁移 1:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import django.utils.timezone


class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Juego',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('nombre_juego', models.CharField(max_length=100)),
                ('record', models.DecimalField(max_digits=10000000, decimal_places=3)),
                ('fecha_inicio', models.DateTimeField(default=django.utils.timezone.now)),
                ('fecha_fin', models.DateTimeField(default=django.utils.timezone.now)),
                ('enlace', models.CharField(max_length=1000)),
            ],
        ),
    ]

迁移 2:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('jugando', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Juegan',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('puntuacion', models.DecimalField(max_digits=10000000, decimal_places=3)),
                ('nombre_juego', models.ManyToManyField(to='jugando.Juego')),
                ('user', models.ManyToManyField(to=settings.AUTH_USER_MODEL)),
            ],
        ),
    ]

运行这个:

brendan@brendan-UX305FA:~/Devel/test/juego$ python manage.py makemigrations
Migrations for 'jugando':
  0002_juegan.py:
    - Create model Juegan
brendan@brendan-UX305FA:~/Devel/test/juego$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, jugando, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying jugando.0002_juegan... OK

问题必须在您显示的 sn-ps 之外的其他地方:可能是数据完整性问题。如果您还没有任何重要数据,您应该考虑使用新数据库重新开始。

【讨论】:

    猜你喜欢
    • 2018-06-10
    • 2015-09-30
    • 2016-01-04
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 2020-03-28
    • 1970-01-01
    相关资源
    最近更新 更多