0

在执行迁移时出现此错误...

(env) PS C:\Users\ashwi\Documents\GitHub\AMCH\AMCH_Recruiter> python manage.py migrate       
Operations to perform:
  Apply all migrations: Recruiter, admin, auth, contenttypes, sessions
Running migrations:
  Applying Recruiter.0003_auto_20211009_2038...Traceback (most recent call last):
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\db\models\fields\__init__.py", line 1823, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: ''

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\ashwi\Documents\GitHub\AMCH\AMCH_Recruiter\manage.py", line 22, in <module>
    main()
  File "C:\Users\ashwi\Documents\GitHub\AMCH\AMCH_Recruiter\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\core\management\base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\core\management\commands\migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\db\migrations\migration.py", line 126, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\db\migrations\operations\fields.py", line 104, in database_forwards
    schema_editor.add_field(
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\db\backends\sqlite3\schema.py", line 330, in add_field
    self._remake_table(model, create_field=field)
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\db\backends\sqlite3\schema.py", line 191, in _remake_table
    self.effective_default(create_field)
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\db\backends\base\schema.py", line 324, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\db\models\fields\__init__.py", line 842, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\db\models\fields\__init__.py", line 2486, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\ashwi\Documents\GitHub\AMCH\env\lib\site-packages\django\db\models\fields\__init__.py", line 1825, in get_prep_value
    raise e.__class__(
ValueError: Field 'cid' expected a number but got ''.

模型.py

from django.db import models
import random
from django.db.models.deletion import CASCADE

from django.db.models.fields import AutoField

class candidate(models.Model):
    fname=models.CharField("First name ",max_length=20,default="")
    lname=models.CharField("Last name ",max_length=20,default="")
    email=models.EmailField("Email ",max_length=254,primary_key=True)
    password=models.CharField("Password ",max_length=100,default="")
    
    def __str__(self):
        return self.email #self.fname+" " +self.lname

class employer(models.Model):
    fname=models.CharField("First name ",max_length=20,default="")
    lname=models.CharField("Last name ",max_length=20,default="")
    email=models.EmailField("Email ",max_length=254,default="")
    password=models.CharField("Password ",max_length=100,default="")

    def __str__(self):
        return self.fname+" " +self.lname


class canDetails(models.Model):
    candEmail=models.ForeignKey(candidate,on_delete=models.CASCADE)
    location=models.CharField("location ",max_length=30)
    role=models.CharField("role ",max_length=20)
    cv=models.FileField(upload_to="media/canDetails/",default="")

    def __str__(self):
        return self.candEmail


class empDetails(models.Model):
    empEmail=models.ForeignKey(candidate,on_delete=models.CASCADE)
    cmpName=models.CharField("Company Name ",max_length=30)
    role=models.CharField("role ",max_length=20)
    cv=models.FileField(upload_to="media/empDetails/",default="")

    def __str__(self):
        return self.empEmail

class jobs(models.Model):
    empEmail=models.ForeignKey(candidate,on_delete=models.CASCADE)
    location=models.CharField("Location ",max_length=30)
    desc=models.CharField("Descriptions ",max_length=3000)
    role=models.CharField("role ",max_length=20)
    sal=models.CharField("salary ",max_length=20)

    def __str__(self):
        return self.empEmail

也许我已经创建了一个名为 cid 的主键,假设这样,我已经删除了我之前创建的模型并重新创建了模型,但我仍然收到这些错误错误来自哪里?任何建议将不胜感激..:>

最新的迁移是 0012_alter_candetails_cv.py

# Generated by Django 3.2.8 on 2021-10-13 19:01

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('Recruiter', '0010_auto_20211014_0024'),
    ]

    operations = [
        migrations.CreateModel(
            name='candidate',
            fields=[
                ('fname', models.CharField(default='', max_length=20, verbose_name='First name ')),
                ('lname', models.CharField(default='', max_length=20, verbose_name='Last name ')),
                ('email', models.EmailField(max_length=254, primary_key=True, serialize=False, verbose_name='Email ')),
                ('password', models.CharField(default='', max_length=100, verbose_name='Password ')),
            ],
        ),
        migrations.CreateModel(
            name='employer',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('fname', models.CharField(default='', max_length=20, verbose_name='First name ')),
                ('lname', models.CharField(default='', max_length=20, verbose_name='Last name ')),
                ('email', models.EmailField(default='', max_length=254, verbose_name='Email ')),
                ('password', models.CharField(default='', max_length=100, verbose_name='Password ')),
            ],
        ),
        migrations.CreateModel(
            name='jobs',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('location', models.CharField(max_length=30, verbose_name='Location ')),
                ('desc', models.CharField(max_length=3000, verbose_name='Descriptions ')),
                ('role', models.CharField(max_length=20, verbose_name='role ')),
                ('sal', models.CharField(max_length=20, verbose_name='salary ')),
                ('empEmail', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Recruiter.candidate')),
            ],
        ),
        migrations.CreateModel(
            name='empDetails',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('cmpName', models.CharField(max_length=30, verbose_name='Company Name ')),
                ('role', models.CharField(max_length=20, verbose_name='role ')),
                ('cv', models.FileField(default='', upload_to='media/empDetails/')),
                ('empEmail', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Recruiter.candidate')),
            ],
        ),
        migrations.CreateModel(
            name='canDetails',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('location', models.CharField(max_length=30, verbose_name='location ')),
                ('role', models.CharField(max_length=20, verbose_name='role ')),
                ('cv', models.FileField(default='', upload_to='media/canDetails/')),
                ('candEmail', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Recruiter.candidate')),
            ],
        ),
    ]

这是我之前创建的迁移cid

0004_alter_candidate_cid.py 类迁移(migrations.Migration):

    dependencies = [
        ('Recruiter', '0003_auto_20211009_2038'),
    ]

    operations = [
        migrations.AlterField(
            model_name='candidate',
            name='cid',
            field=models.AutoField(default=101, primary_key=True, serialize=False, verbose_name='candidate id'),
        ),
    ]











# Generated by Django 3.2.8 on 2021-10-09 15:08

from django.db import migrations, models


class Migration(migrations.Migration):

0003_auto_20211009_2038.cpython-39.py

dependencies = [
        ('Recruiter', '0002_auto_20211009_2003'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='candidate',
            name='id',
        ),
        migrations.AddField(
            model_name='candidate',
            name='cid',
            field=models.AutoField(default='', primary_key=True, serialize=False, verbose_name='candidate id'),
        ),
        migrations.AlterField(
            model_name='candetails',
            name='file',
            field=models.FileField(default='', max_length=254, upload_to='', verbose_name='CV '),
        ),
        migrations.AlterField(
            model_name='candetails',
            name='location',
            field=models.CharField(default='', max_length=20, verbose_name='Location '),
        ),
        migrations.AlterField(
            model_name='candetails',
            name='role',
            field=models.CharField(default='', max_length=20, verbose_name='role '),
        ),
        migrations.AlterField(
            model_name='employer',
            name='email',
            field=models.EmailField(default='', max_length=254, verbose_name='Email '),
        ),
        migrations.AlterField(
            model_name='employer',
            name='fname',
            field=models.CharField(default='', max_length=20, verbose_name='First name '),
        ),
        migrations.AlterField(
            model_name='employer',
            name='lname',
            field=models.CharField(default='', max_length=20, verbose_name='Last name '),
        ),
        migrations.AlterField(
            model_name='employer',
            name='password',
            field=models.CharField(default='', max_length=100, verbose_name='Password '),
        ),
    ]
4

1 回答 1

0

编辑您的迁移文件0003_auto_20211009_2038.cpython-39.py

migrations.AddField(
            model_name='candidate',
            name='cid',
            field=models.AutoField(default=1, primary_key=True, serialize=False, verbose_name='candidate id'),
        ),

并执行迁移。

于 2021-10-13T20:40:45.237 回答