我正在尝试开发一个网络应用程序,可以在django上创建一个迷你游戏联盟。在创建数据库时,我收到以下错误:
你试图在没有默认值的情况下向juego添加一个不可为空的字段'id';我们不能这样做(数据库需要填充现有行的东西)。
以下是文件Juego
上的models.py
课程:
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)
答案 0 :(得分:0)
您现在拥有的代码没有任何问题。我刚做了一个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
问题必须在您展示的片段之外的其他地方:可能是数据完整性问题。如果您还没有任何重要数据,则应考虑重新使用新数据库。