psycopg2.DataError:整数的无效输入语法:“ test”;将代码移至测试服务器时出现错误 [英] psycopg2.DataError: invalid input syntax for integer: "test" Getting error when moving code to test server

查看:137
本文介绍了psycopg2.DataError:整数的无效输入语法:“ test”;将代码移至测试服务器时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ubuntu 14.04.5上运行带Python 3.4的Django 1.11

I'm running Django 1.11 with Python 3.4 on Ubuntu 14.04.5

将开发代码移至测试服务器并遇到一些奇怪的错误。谁能从追溯中看到问题所在?

Moving my development code to the test server and running into some strange errors. Can anyone see what is wrong from the traceback?

我对linux还是很陌生,在第一次尝试Windows机器时就犯了错误。此后,我已经创建了要在其上进行开发的测试服务器和生产服务器的virtualbox副本,但我希望现在可以挽救测试服务器上的最新信息。

I'm very new to linux and have made the mistake of developing on a Windows machine on this first go around. I have since created a virtualbox copy of the test and production servers to develop on, but I'm hoping I can salvage what's up on the test server now.

我的应用程序正在此环境中查找正确的目录,但是我是Django,Python和linux noob。

I think my app is looking in the correct directory for this environment, but I am a Django, Python and linux noob.

任何方向都会很有帮助。

Any direction would be very helpful.

**更新:我为相关应用添加了models.py和迁移。另外,我在开发机上使用sqlite,在测试服务器上使用postgreSQL(像傻瓜一样)。

**UPDATE: I added models.py and migration for relevant app. Also, I was using sqlite on dev machine and am using postgreSQL on test server (like a fool).

谢谢!
staff_manager / models.py

Thanks! staff_manager/models.py

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

# Create your models here.

from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from smrt.settings import DATE_INPUT_FORMATS


class OrganizationTitle(models.Model):
    def __str__(self):
        return "{}".format(self.organization_title_name)
    organization_title_name = models.CharField(max_length=150, unique=True)


class ClassificationTitle(models.Model):
    def __str__(self):
        return "{}".format(self.classification_title_name)
    classification_title_name = models.CharField(max_length=150, unique=True)


class WorkingTitle(models.Model):
    def __str__(self):
        return "{}".format(self.working_title_name)
    working_title_name = models.CharField(max_length=150, unique=True)


class Category(models.Model):
    def __str__(self):
        return "{}".format(self.category_name)
    category_name = models.CharField(max_length=150, unique=True)


class Department(models.Model):
    def __str__(self):
        return "{}".format(self.department_name)
    department_name = models.CharField(max_length=150, unique=True)


class Employee(models.Model):
    first_name = models.CharField(max_length=150)
    last_name = models.CharField(max_length=150)
    org_title = models.ForeignKey(OrganizationTitle, blank=True, null=True, on_delete=models.SET_NULL)
    manager = models.ForeignKey('self', blank=True, null=True, on_delete=models.SET_NULL)
    manager_email = models.EmailField(max_length=50, blank=True, null=True)
    hire_date = models.DateField(blank=True, null=True)
    classification_title = models.ForeignKey(ClassificationTitle, blank=True, null=True, on_delete=models.SET_NULL)
    working_title = models.ForeignKey(WorkingTitle, blank=True, null=True, on_delete=models.SET_NULL)
    email_address = models.EmailField(max_length=250, blank=False, unique=True,
                                      error_messages={'unique': 'An account with this email exist.',
                                                      'required': 'Please provide an email address.'})
    category = models.ForeignKey(Category, blank=True, null=True, on_delete=models.SET_NULL)
    is_substitute = models.BooleanField(default=False)
    department = models.ForeignKey(Department, blank=True, null=True, on_delete=models.SET_NULL)
    is_active = models.BooleanField(default=True)
    is_manager = models.BooleanField(default=False)

    class Meta:
        ordering = ('is_active', 'last_name',)

    def __str__(self):
        return "{}".format(self.first_name + ' ' + self.last_name)

    def __iter__(self):
        return iter([
                     self.email_address,
                     self.last_name,
                     self.first_name,
                     self.org_title,
                     self.manager,
                     self.manager.email_address,
                     self.hire_date,
                     self.classification_title,
                     self.working_title,
                     self.email_address,
                     self.category,
                     self.is_substitute,
                     self.department
                     ])

    def save(self, *args, **kwargs):
        for field_name in ['first_name', 'last_name']:
            val = getattr(self, field_name, False)
            if val:
                setattr(self, field_name, val.capitalize())
        super(Employee, self).save(*args, **kwargs)

MIGRATION staff_manager.0003_auto_20180131_1756:

MIGRATION staff_manager.0003_auto_20180131_1756:

# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2018-01-31 17:56
from __future__ import unicode_literals

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


class Migration(migrations.Migration):

    dependencies = [
        ('staff_manager', '0002_auto_20171127_2244'),
    ]

    operations = [
        migrations.CreateModel(
            name='Category',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('category_name', models.CharField(max_length=150, unique=True)),
            ],
        ),
        migrations.CreateModel(
            name='ClassificationTitle',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('classification_title_name', models.CharField(max_length=150, unique=True)),
            ],
        ),
        migrations.CreateModel(
            name='Department',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('department_name', models.CharField(max_length=150, unique=True)),
            ],
        ),
        migrations.CreateModel(
            name='OrganizationTitle',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('organization_title_name', models.CharField(max_length=150, unique=True)),
            ],
        ),
        migrations.CreateModel(
            name='WorkingTitle',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('working_title_name', models.CharField(max_length=150, unique=True)),
            ],
        ),
        migrations.AlterField(
            model_name='employee',
            name='category',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='staff_manager.Category'),
        ),
        migrations.AlterField(
            model_name='employee',
            name='classification_title',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='staff_manager.ClassificationTitle'),
        ),
        migrations.AlterField(
            model_name='employee',
            name='department',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='staff_manager.Department'),
        ),
        migrations.AlterField(
            model_name='employee',
            name='email_address',
            field=models.EmailField(error_messages={'required': 'Please provide an email address.', 'unique': 'An account with this email exist.'}, max_length=250, unique=True),
        ),
        migrations.AlterField(
            model_name='employee',
            name='first_name',
            field=models.CharField(max_length=150),
        ),
        migrations.AlterField(
            model_name='employee',
            name='hire_date',
            field=models.DateField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='employee',
            name='last_name',
            field=models.CharField(max_length=150),
        ),
        migrations.AlterField(
            model_name='employee',
            name='manager',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='staff_manager.Employee'),
        ),
        migrations.AlterField(
            model_name='employee',
            name='manager_email',
            field=models.EmailField(blank=True, max_length=50, null=True),
        ),
        migrations.AlterField(
            model_name='employee',
            name='org_title',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='staff_manager.OrganizationTitle'),
        ),
        migrations.AlterField(
            model_name='employee',
            name='working_title',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='staff_manager.WorkingTitle'),
        ),
    ]

回溯:

Operations to perform:
      Apply all migrations: admin, auth, contenttypes, csvimport, sessions, staff_manager
    Running migrations:
      Applying staff_manager.0003_auto_20180131_1756...Traceback (most recent call last):
      File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute
        return self.cursor.execute(sql, params)
    psycopg2.DataError: invalid input syntax for integer: "test"

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

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 515, in alter_field
    old_db_params, new_db_params, strict)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/backends/postgresql/schema.py", line 112, in _alter_field
    new_db_params, strict,
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 684, in _alter_field
    params,
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 120, in execute
    cursor.execute(sql, params)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/backends/utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.DataError: invalid input syntax for integer: "test"

(django_env_1) www-root@Server:~/envs/django_env_1/smrt$
^C
(django_env_1) www-root@Server:~/envs/django_env_1/smrt$ django.db.utils.DataError: invalid input syntax for integer: "test"django.db.utils.DataError: invalid input syntax for integer: "test"


推荐答案

该问题可能与Django中的此打开的错误有关。您现在正在转换为ForeignKey的字段之一中有一些测试数据。

The issue is likely related to this open bug in Django. You have some test data in one of the fields that you are now converting to a ForeignKey.

例如,也许部门曾经是 CharField ,并且您添加了一个员工,该雇员的部门的值是 test。现在,您尝试将部门从CharField更改为ForeignKey。问题是Django试图将前一个值 test转换为ForeignKey的关系值(整数)。

For instance, maybe department used to be a CharField and you added an employee who has "test" as their department value. Now you're trying to change department from a CharField to a ForeignKey. The issue is that Django is trying to convert the previous value "test" into a relational value (integer) for the ForeignKey.

我能想到一些好的解决方案:

I can think of a few good solutions:


  • 如果这只是一个测试数据库,只需重置数据库并在干净的数据库上运行迁移

  • 如果需要迁移现有数据,请找出哪个字段具有测试值。然后尝试类似于错误报告中给出的解决方案:

```

from __future__ import unicode_literals

from django.db import migrations

class Migration(migrations.Migration):
    dependencies = [
        ('documents', '0042_auto_19700101-0000'),
    ]

    operations = [
        migrations.RunSQL('ALTER TABLE documents_document_tags ALTER tag_id TYPE varchar(32);'),
    ]

这篇关于psycopg2.DataError:整数的无效输入语法:“ test”;将代码移至测试服务器时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆