简体   繁体   English

Django:迁移时遇到 OperationalError

[英]Django: encounter an OperationalError when migrate

I encountered this error when deployed my code on a ubuntu server.我在 ubuntu 服务器上部署我的代码时遇到了这个错误。 I have tested the migration locally (macOS Mojave) and made no mistakes.我已经在本地(macOS Mojave)测试了迁移并且没有出错。

I tried to delete all migration files except the __intit__.py , but Django gave the same error.我试图删除除__intit__.py之外的所有迁移文件,但 Django 给出了同样的错误。

The error traceback (home is the name of my app):错误回溯(home 是我的应用程序的名称):

Applying home.0001_initial...Traceback (most recent call last):
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 381, in execute
    return Database.Cursor.execute(self, query)
sqlite3.OperationalError: no such column: REFERRING.S

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

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
    fake_initial=fake_initial,
  File "/home/admin/sites/site1/venv/lib/python3.6/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 "/home/admin/sites/site1/venv/lib/python3.6/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 "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 247, in apply_migration
    migration_recorded = True
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 34, in __exit__
    self.connection.check_constraints()
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 341, in check_constraints
    column_name, referenced_column_name,
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
  File "/home/admin/sites/site1/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 381, in execute
    return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: no such column: REFERRING.S

I don't have a column named REFERRING.S in my model explicitly, so I don't know what's happening here at all.我的模型中没有明确命名为 REFERRING.S 的列,所以我根本不知道这里发生了什么。

Edit:编辑:

Finally, I managed to make a minimal case to reproduce this error.最后,我设法制作了一个最小的案例来重现此错误。 It seems that this error is due to somehow like a database injection...似乎这个错误是由于某种类似于数据库注入......

Here is the minimal case to reproduce this error.这是重现此错误的最小情况。

from django.db import models


class Customer(models.Model):
    name = models.CharField(max_length=255, unique=True)


class Order(models.Model):
    orderNum = models.CharField(max_length=14, unique=True) 
    customer_idx = models.ForeignKey(Customer, on_delete=models.CASCADE, default=1)  

    class Meta:
        db_table = 'xxx(S)'

I have a model Customer and a model Order with the db_table named 'xxx(S)'.我有一个模型Customer和一个模型Order ,其 db_table 名为“xxx(S)”。 Maybe the "(S)" triggers some weird behavior.也许“(S)”会触发一些奇怪的行为。 I'm a newbie to SQL and django.我是 SQL 和 django 的新手。 Could someone help me to explain this?有人可以帮我解释一下吗?

I'm using django 3.0.3 (and 2.2.5 also failed) and ubuntu 16.04.我正在使用 django 3.0.3(并且 2.2.5 也失败了)和 ubuntu 16.04。 And this piece of code seems to work fine on MacOS.这段代码在 MacOS 上似乎运行良好。

Edit 2:编辑2:

The migration script: 0001_initial.py迁移脚本:0001_initial.py

# Generated by Django 3.0.3 on 2020-02-21 16:49

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Customer',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=255, unique=True)),
            ],
        ),
        migrations.CreateModel(
            name='Order',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('orderNum', models.CharField(max_length=255, unique=True)),
                ('customer_idx', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='home.Customer')),
            ],
            options={
                'db_table': 'xxx(S)',
            },
        ),
    ]

You're referring the model Order to datable xxx(S) .您将模型Order称为 datatable xxx(S) Since the model Order is in relationship with Customer model, Customer model also should be there in same database xxx(S) .由于模型OrderCustomer模型相关,因此Customer模型也应该存在于同一个数据库xxx(S)

Create meta class to Customer and refer to same database.为 Customer 创建元类并引用相同的数据库。

class Meta:
    db_table = 'xxx(S)'

I finally figured it out.我终于弄明白了。 The bracket in sql means column selection, so the table name "xxx(S)" were interpreted as select the column "S" of table "xxx". sql中的括号表示列选择,所以表名“xxx(S)”被解释为选择表“xxx”的“S”列。 So just change the table name fixed the issue.所以只需更改表名即可解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用 docker 迁移时出现 django OperationalError - django OperationalError when trying to migrate with docker 使用 PostgreSQL 9.5 在 Django 1.9 中执行“python manage.py migrate”时出现错误“django.db.utils.OperationalError” - Getting error 'django.db.utils.OperationalError' when doing 'python manage.py migrate' in Django 1.9 with PostgreSQL 9.5 Django迁移django.db.utils.OperationalError:没有这样的表: - Django migrate django.db.utils.OperationalError: no such table: makemigrations/migrate 错误 django.db.utils.OperationalError:没有这样的表 - makemigrations/migrate error django.db.utils.OperationalError: no such table flask db 迁移 - OperationalError - flask db migrate - OperationalError Django:在 Docker 上连接到 PostgreSQL 数据库时出现 OperationalError - Django: OperationalError when connecting to PostgreSQL DB on Docker django 操作错误 - django OperationalError Django 迁移:django.db.utils.OperationalError:(1364,“字段‘名称’没有默认值”) - Django migrate: django.db.utils.OperationalError: (1364, "Field 'name' doesn't have a default value") 尝试在Django 1.9中迁移 - 奇怪的SQL错误“django.db.utils.OperationalError:near”)“:语法错误” - Trying to migrate in Django 1.9 — strange SQL error “django.db.utils.OperationalError: near ”)“: syntax error” 遇到 DetailView 时更新模型字段。 [姜戈] - Update a Model Field when DetailView encounter. [Django]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM