2015-10-19 24 views
2

我正在嘗試開發一個Web應用程序,該程序提供了在django上創建迷你遊戲聯盟。目前,當創建數據庫我收到以下錯誤:您正試圖在模型中添加一個不可爲空的字段'id'

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).

這裏是我的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

自遊覽最後一次遷移以來,您還完成了哪些工作? – Wtower

+0

我將以下課程添加到模型中: – rafaelleru

+1

請使用編輯鏈接在您的問題中提供更多詳細信息,並避免在註釋中添加它們! – Wtower

回答

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)), 
      ], 
     ), 
    ] 

跑了這一點:

[email protected]:~/Devel/test/juego$ python manage.py makemigrations 
Migrations for 'jugando': 
    0002_juegan.py: 
    - Create model Juegan 
[email protected]:~/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 

該問題必須出現在您顯示的片段之外的某處:可能是數據完整性問題。如果你還沒有任何重要的數據,你應該考慮重新開始一個新的數據庫。

相關問題