2014-11-06 103 views
0

我的模型遷移到數據庫時遇到問題。遷移期間出錯

models.py:

# coding: utf-8 
from django.db import models 
# Create your models here. 

class Product(models.Model): 
    title = models.CharField('title', max_length=100, blank=False) 
    author = models.CharField('author', max_length=100, blank=False) 

    def __unicode__(self): 
     return "%s - %s" % (self.title,self.author) 

    class Meta: 
     verbose_name = 'Produkt' 
     verbose_name_plural = 'Produkty' 

這是錯誤日誌:

Operations to perform: 
    Synchronize unmigrated apps: django_extensions 
    Apply all migrations: admin, TestApp, contenttypes, auth, sessions 
Synchronizing apps without migrations: 
    Creating tables... 
    Installing custom SQL... 
    Installing indexes... 
Running migrations: 
    Applying contenttypes.0001_initial... OK 
    Applying auth.0001_initial... OK 
    Applying TestApp.0001_initial... OK 
    Applying TestApp.0002_auto_20141104_1305... OK 
    Applying TestApp.0003_auto_20141104_1312... OK 
    Applying TestApp.0004_genretype_producttype... OK 
    Applying TestApp.0005_auto_20141104_1322... OK 
    Applying TestApp.0006_auto_20141104_1338... OK 
    Applying TestApp.0007_product_producttype... OK 
    Applying TestApp.0008_auto_20141106_0852...Traceback (most recent call last): 
    File "C:\Program Files (x86)\JetBrains\PyCharm 3.4.1\helpers\pycharm\django_manage.py", line 23, in <module> 
    run_module(manage_file, None, '__main__', True) 
    File "C:\Python27\lib\runpy.py", line 176, in run_module 
    fname, loader, pkg_name) 
    File "C:\Python27\lib\runpy.py", line 82, in _run_module_code 
    mod_name, mod_fname, mod_loader, pkg_name) 
    File "C:\Python27\lib\runpy.py", line 72, in _run_code 
    exec code in run_globals 
    File "C:\Users\Martin\PycharmProjects\SPJA_PROJECT_Videoshop\manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line 
    utility.execute() 
    File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 377, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "C:\Python27\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv 
    self.execute(*args, **options.__dict__) 
    File "C:\Python27\lib\site-packages\django\core\management\base.py", line 338, in execute 
    output = self.handle(*args, **options) 
    File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 160, in handle 
    executor.migrate(targets, plan, fake=options.get("fake", False)) 
    File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 63, in migrate 
    self.apply_migration(migration, fake=fake) 
    File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 97, in apply_migration 
    migration.apply(project_state, schema_editor) 
    File "C:\Python27\lib\site-packages\django\db\migrations\migration.py", line 107, in apply 
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state) 
    File "C:\Python27\lib\site-packages\django\db\migrations\operations\fields.py", line 139, in database_forwards 
    schema_editor.alter_field(from_model, from_field, to_field) 
    File "C:\Python27\lib\site-packages\django\db\backends\schema.py", line 473, in alter_field 
    self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict) 
    File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\schema.py", line 190, in _alter_field 
    self._remake_table(model, alter_fields=[(old_field, new_field)]) 
    File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\schema.py", line 128, in _remake_table 
    self.create_model(temp_model) 
    File "C:\Python27\lib\site-packages\django\db\backends\schema.py", line 209, in create_model 
    definition, extra_params = self.column_sql(model, field) 
    File "C:\Python27\lib\site-packages\django\db\backends\schema.py", line 121, in column_sql 
    default_value = self.effective_default(field) 
    File "C:\Python27\lib\site-packages\django\db\backends\schema.py", line 184, in effective_default 
    default = field.get_db_prep_save(default, self.connection) 
    File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 627, in get_db_prep_save 
    prepared=False) 
    File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1176, in get_db_prep_value 
    value = self.get_prep_value(value) 
    File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1171, in get_prep_value 
    return self.to_python(value) 
    File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1128, in to_python 
    parsed = parse_date(value) 
    File "C:\Python27\lib\site-packages\django\utils\dateparse.py", line 37, in parse_date 
    match = date_re.match(value) 
TypeError: expected string or buffer 

Process finished with exit code 1 

我與Django的初學者和沒有太多的經驗。任何人都可以說我,我做錯了什麼?錯誤在哪裏?我試着在最近兩個小時內找到它,沒有任何東西。

這是不正常的遷移:

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

from django.db import models, migrations 


class Migration(migrations.Migration): 

    dependencies = [ 
     ('TestApp', '0007_product_producttype'), 
    ] 

    operations = [ 
     migrations.RemoveField(
      model_name='product', 
      name='author', 
     ), 
     migrations.AddField(
      model_name='product', 
      name='director', 
      field=models.CharField(default=b'', max_length=100, verbose_name=b'director'), 
      preserve_default=True, 
     ), 
     migrations.AlterField(
      model_name='product', 
      name='genre', 
      field=models.OneToOneField(default=b'', to='TestApp.GenreType'), 
      preserve_default=True, 
     ), 
     migrations.AlterField(
      model_name='product', 
      name='mediatype', 
      field=models.OneToOneField(default=b'', to='TestApp.MediaType'), 
      preserve_default=True, 
     ), 
     migrations.AlterField(
      model_name='product', 
      name='year', 
      field=models.DateField(default=2000, verbose_name=b'year'), 
      preserve_default=True, 
     ), 
    ] 
+0

** countfree **是什麼? – 2014-11-06 10:25:26

+0

錯誤,我現在編輯了原始代碼 – MarMar 2014-11-06 10:28:00

+1

你可以把遷移文件的內容** 0008_auto_20141106_0852 **嗎? – 2014-11-06 10:28:19

回答

0

你有一個無效的遷移0008_auto_20141106_0852:你有一個DateField

migrations.AlterField(
    model_name='product', 
    name='year', 
    field=models.DateField(default=2000, verbose_name=b'year'), 
    preserve_default=True, 
), 

,但默認值是一個整數。你需要從DateField本場更改爲IntegerField,然後再次運行遷移:

migrations.AlterField(
    model_name='product', 
    name='year', 
    field=models.IntegerField(default=2000, verbose_name=b'year'), 
    preserve_default=False, 
), 

進行此項更改,您應該遷移,做工精細。

+0

謝謝!有用!!! – MarMar 2014-11-06 10:42:22