gpt4 book ai didi

python - 您正在尝试向模型添加不可为空的字段 'id'

转载 作者:行者123 更新时间:2023-12-05 07:52:48 25 4
gpt4 key购买 nike

我正在尝试开发一个 Web 应用程序,它可以在 Django 上创建一个迷你游戏联盟。在创建数据库时出现以下错误:

You are trying to add a non-nullable field 'id' to juego without a default; we can't do that (the database needs something to populate existing rows).

这是文件 models.py 上我的类(class) 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)

最佳答案

您现在的代码没有任何问题。我刚刚做了一个 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

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

关于python - 您正在尝试向模型添加不可为空的字段 'id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33219207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com