首页 > 解决方案 > SQLDecodeError(err_key=tok.value, djongo.exceptions.SQLDecodeError

问题描述

我正在使用mongoDB在Django中开展一个项目。我的应用程序的名称是.app

我的models.py

class myCustomeUser(AbstractUser):
    #id = models.AutoField(primary_key=True)
    username = models.CharField(max_length=20, unique="True", blank=False)
    password = models.CharField(max_length=20, blank=False)
    is_Employee = models.BooleanField(default=False)
    is_Inspector = models.BooleanField(default=False)
    is_Industry = models.BooleanField(default=False)
    is_Admin = models.BooleanField(default=False)

    def __str__(self):
        return self.username

class Industry(models.Model):
    user = models.OneToOneField(myCustomeUser, on_delete=models.CASCADE, primary_key=True, related_name='industry_releted_user')
    name = models.CharField(max_length=200, blank=True)
    owner = models.CharField(max_length=200, blank=True)
    license = models.IntegerField(null=True, unique=True)
    industry_extrafield = models.TextField(blank=True)

    def __str__(self):
        return self.name

class Employee(models.Model):
    user = models.ForeignKey(myCustomeUser, null=True, blank=True, on_delete=models.CASCADE)
    industry = models.ForeignKey(Industry, null=True, blank=True, on_delete=models.CASCADE)
    i_id = models.IntegerField(unique=True)
    name = models.CharField(max_length=200, blank=False, null=True)
    gmail = models.EmailField(null=True, blank=False, unique=True)
    rank = models.CharField(max_length=20, blank=False, null=True)
    employee_varified = models.BooleanField(default=False, blank=True, null=True)

    def __str__(self):
        return self.name

class Inspector(models.Model):
    user = models.ForeignKey(myCustomeUser, on_delete=models.CASCADE, related_name='inspector_releted_user')
    name = models.CharField(max_length=200, blank=False, null=True)
    gmail = models.EmailField(null=True, blank=False, unique=True)
    nationalid = models.IntegerField(null=True, blank=False, unique=True)
    inspector_extrafield = models.TextField(blank=True)

    def __str__(self):
        return self.name

class Admin(models.Model):
    user = models.ForeignKey(myCustomeUser, on_delete=models.CASCADE, related_name='admin_releted_user')
    name = models.CharField(max_length=200, blank=False, null=True)
    gmail = models.EmailField(null=True, blank=False, unique=True)
    admin_extrafield = models.TextField(blank=True)

    def __str__(self):
        return self.name

class PrevJob(models.Model):
    employee = models.ForeignKey(Employee, null=True, blank=True, on_delete=models.CASCADE)
    industry = models.ForeignKey(Industry, null=True, blank=True, on_delete=models.CASCADE)
    i_id = models.IntegerField(unique=True)
    #name = models.CharField(max_length=200, blank=False, null=True)
    rank = models.CharField(max_length=20, blank=False, null=True)

    def __str__(self):
        return self.employee.name

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'ProjectSurokkhaBetaDB_5',
    }
}

今天早上我删除了我的完整数据库(并创建了一个全新的数据库)并删除了里面的所有文件app\migrations(除了__init__.py)。跑makemigrations,然后我跑migrate。但是运行migrate命令后,它在终端中显示以下错误:

Operations to perform:
  Apply all migrations: admin, app, auth, contenttypes, sessions
Running migrations:
This version of djongo does not support "NULL, NOT NULL column validation check" fully. Visit https://www.patreon.com/nesdis
  Applying contenttypes.0001_initial...This version of djongo does not support "schema validation using CONSTRAINT" fully. Visit https://www.patreon.com/nesdis
 OK
  Applying contenttypes.0002_remove_content_type_name...This version of djongo does not support "COLUMN DROP NOT NULL " fully. Visit https://www.patreon.com/nesdis
This version of djongo does not support "DROP CASCADE" fully. Visit https://www.patreon.com/nesdis
 OK
  Applying auth.0001_initial...This version of djongo does not support "schema validation using KEY" fully. Visit 
https://www.patreon.com/nesdis
This version of djongo does not support "schema validation using REFERENCES" fully. Visit https://www.patreon.com/nesdis
 OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
Not implemented alter command for SQL ALTER TABLE "app_employee" ADD COLUMN "industry_id" int NULL
  Applying app.0001_initial...Traceback (most recent call last):
  File "G:\Python\lib\site-packages\djongo\cursor.py", line 51, in execute
    self.result = Query(
  File "G:\Python\lib\site-packages\djongo\sql2mongo\query.py", line 783, in __init__
    self._query = self.parse()
  File "G:\Python\lib\site-packages\djongo\sql2mongo\query.py", line 875, in parse
    raise e
  File "G:\Python\lib\site-packages\djongo\sql2mongo\query.py", line 856, in parse
    return handler(self, statement)
  File "G:\Python\lib\site-packages\djongo\sql2mongo\query.py", line 888, in _alter
    query = AlterQuery(self.db, self.connection_properties, sm, self._params)
  File "G:\Python\lib\site-packages\djongo\sql2mongo\query.py", line 425, in __init__
    super().__init__(*args)
  File "G:\Python\lib\site-packages\djongo\sql2mongo\query.py", line 84, in __init__
    super().__init__(*args)
  File "G:\Python\lib\site-packages\djongo\sql2mongo\query.py", line 62, in __init__
    self.parse()
  File "G:\Python\lib\site-packages\djongo\sql2mongo\query.py", line 435, in parse
    self._add(statement)
  File "G:\Python\lib\site-packages\djongo\sql2mongo\query.py", line 598, in _add
    raise SQLDecodeError(err_key=tok.value,
djongo.exceptions.SQLDecodeError:

        Keyword: int
        Sub SQL: ALTER TABLE "app_employee" ADD COLUMN "industry_id" int NULL
        FAILED SQL: ('ALTER TABLE "app_employee" ADD COLUMN "industry_id" int NULL',)
        Params: ([],)
        Version: 1.3.3

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

Traceback (most recent call last):
  File "G:\Python\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "G:\Python\lib\site-packages\djongo\cursor.py", line 59, in execute
    raise db_exe from e
djongo.database.DatabaseError

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 "G:\Python\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line   
    utility.execute()
  File "G:\Python\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "G:\Python\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "G:\Python\lib\site-packages\django\core\management\base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "G:\Python\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "G:\Python\lib\site-packages\django\core\management\commands\migrate.py", line 231, in handle
    post_migrate_state = executor.migrate(
  File "G:\Python\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 "G:\Python\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 "G:\Python\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "G:\Python\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "G:\Python\lib\site-packages\django\db\migrations\operations\fields.py", line 110, in database_forwards    
    schema_editor.add_field(
  File "G:\Python\lib\site-packages\django\db\backends\base\schema.py", line 480, in add_field
    self.execute(sql, params)
  File "G:\Python\lib\site-packages\django\db\backends\base\schema.py", line 142, in execute
    cursor.execute(sql, params)
  File "G:\Python\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "G:\Python\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "G:\Python\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "G:\Python\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "G:\Python\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "G:\Python\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "G:\Python\lib\site-packages\djongo\cursor.py", line 59, in execute
    raise db_exe from e
django.db.utils.DatabaseError

但这里最有趣的部分是,当我从Django的 defaultmongoDB转换时,没有错误,并且所有代码都使用相同的代码顺利运行。所以可能使用有问题。sqlite3mongoDB

mongoDB转移sqlite3代码settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

我以前从未遇到过这种奇怪的问题。我怎样才能解决这个问题?

标签: pythondjangomongodb

解决方案


推荐阅读