简体   繁体   English

在django中迁移模型时如何解决SQLdecode错误?

[英]How to solve SQLdecode error when you migrate models in django?

I am new to django, I have created a project and app and I would like to connect my project to the mongodb.我是 django 的新手,我创建了一个项目和应用程序,我想将我的项目连接到 mongodb。 when I enter python manage.py migrate command , I am getting below mentioned error.当我输入python manage.py migrate命令时,我遇到了下面提到的错误。

I have dropped database and cleared all migrations in the django_migration table and deleted migration files in the created migrations folder.我删除了数据库并清除了 django_migration 表中的所有迁移,并删除了创建的迁移文件夹中的迁移文件。 Still getting same error.仍然得到同样的错误。

Please help me with this.请帮我解决一下这个。 Thanks in advance提前致谢

Error:错误:

    raise TypeError("documents must be a non-empty list")
TypeError: documents must be a non-empty list

The above exception was the direct cause of the following exception:上述异常是以下异常的直接原因:

djongo.sql2mongo.SQLDecodeError: FAILED SQL: INSERT INTO "django_migrations" ("app", "name", "applied") VALUES (%(0)s, %(1)s, %(2)s)
    Version: 1.2.31

Settings.py设置.py

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'DB_name',
        'HOST':'localhost',
        # 'PORT': 27017,
        # 'USER':'',
        # 'PASSWORD':''
    },

models.py模型.py

from django.db import models

# Create your models here.
class dummy(models.Model):

    name= models.CharField(max_length=100, blank=True, null= True)

After creating django project uninstall sqlparse by typing创建 django 项目后,通过键入卸载sqlparse

pip3 uninstall sqlparse

Then install sqlparse version=0.2.4 by typing然后通过键入安装 sqlparse version=0.2.4

pip3 install sqlparse==0.2.4

Then migrate mongodb database by typing然后通过键入迁移mongodb数据库

python3 manage.py migrate

If you are using python2 then use ( pip ) instead of ( pip3 ) and ( python ) instead of ( python3 ) in the command.如果您使用的是python2,则在命令中使用 ( pip ) 而不是 ( pip3 ) 和 ( python ) 而不是 ( python3 ) 。

I have solved this problem.我已经解决了这个问题。 The problem may be the version of sqlparse - sqlparse 0.2.4 works well, but sqlparse 0.3.0 not.问题可能是 sqlparse 的版本 - sqlparse 0.2.4 运行良好,但 sqlparse 0.3.0 不行。 Use following command:使用以下命令:

pip install sqlparse==0.2.4 --user

我已经用pip install --upgrade djongo更新了pip install --upgrade djongo并且我已经完成了迁移python manage.py migrate --fake-initial并且它已经被修复。

Just install sqlprase to solve this.只需安装sqlprase即可解决此问题。

Download it github sqlprase下载github sqlprase

pip install filename.zip

Use python manage.py migrate --fake-initial the first time you migrate your project.第一次迁移项目时使用python manage.py migrate --fake-initial

In setting.py add INSTALLED_APPS=[ 'APP_NAME.apps.APP_NAMEConfig', ..., ..., ]在 setting.py 添加INSTALLED_APPS=[ 'APP_NAME.apps.APP_NAMEConfig', ..., ..., ]

我已经这样解决了sqlparse==0.2.4 and djongo==1.3.3 and django==2.1.7

请尝试在终端中使用此命令安装 sqlparse 包。

 pip install sqlparse==0.2.4 

I have the same issue with u, I tried with sqlite3 as the database, and the data is just like this:我和你有同样的问题,我尝试用 sqlite3 作为数据库,数据是这样的:

在此处输入图片说明

so the data is not empty!!所以数据不为空!!

And I tried to change the sql command as follow(line 760 at sql2mongo\\query.py):我尝试按如下方式更改 sql 命令(sql2mongo\\query.py 中的第 760 行):

statement = statement[0]
    sm_type = statement.get_type()
    if sm_type=='INSERT':
        self._sql = 'INSERT INTO "django_migrations" ("app", "name", "applied") VALUES ("test", "windy", "2019-03-15 12:00")'

but still failed.但还是失败了。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM