Djonog:存储 ArrayField 会产生不明确的错误 'django.db.utils.DatabaseError'
Djonog: storing an ArrayField gives an ambiguous error 'django.db.utils.DatabaseError'
我正在尝试将 ArrayField
存储在名为 User
的 Django 模型中,我正在使用 MongoDB 和 Djongo。
我设法用一个名为 Pair
的模型做到了,我可以存储 Pair
的列表并使用 ArrayField
将该列表存储在 User
模型中这相当简单,我只是从 Djongo 的官方文档
中关注 this example
现在我正在尝试存储另一个列表,但出于某种原因,它一直给我一个巨大的 TraceBack(90 行)和一个不明确的错误:'django.db.utils.DatabaseError'
这是我的用户模型:
class User(AbstractBaseUser):
email = models.EmailField(verbose_name='email address', max_length=127, unique=True)
first_name = models.CharField(verbose_name='First Name', max_length=30, blank=True)
last_name = models.CharField(verbose_name='Last Name', max_length=30, blank=True)
is_active = models.BooleanField(default=True, verbose_name='Active')
is_admin = models.BooleanField(default=False, verbose_name='Admin')
pairs = models.ArrayField (
model_container=Pair,
null=True
)
conversations = models.ArrayField(
model_container=Conversation,
null=True
)
同样,pairs 属性工作正常,但对话不正常
Pair
模型
class Pair(models.Model):
name = models.CharField(max_length=16)
purchase_price = models.DecimalField(verbose_name='Purchase price', max_digits=20, decimal_places=6)
class Meta:
ordering = ['name']
abstract=True
Conversation
模型
class Conversation(models.Model):
handle = models.CharField(max_length=255)
class Meta:
abstract = True
这是追溯:
Operations to perform:
Apply all migrations: accounts, admin, auth, contenttypes, sessions, tracker
Running migrations:
Not implemented alter command for SQL ALTER TABLE "accounts_user" ADD COLUMN "conversations" array NULL
Applying accounts.0003_user_conversations...Traceback (most recent call last):
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/cursor.py", line 51, in execute
self.result = Query(
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 783, in __init__
self._query = self.parse()
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 875, in parse
raise e
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 856, in parse
return handler(self, statement)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 888, in _alter
query = AlterQuery(self.db, self.connection_properties, sm, self._params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 425, in __init__
super().__init__(*args)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 84, in __init__
super().__init__(*args)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 62, in __init__
self.parse()
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 435, in parse
self._add(statement)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 598, in _add
raise SQLDecodeError(err_key=tok.value,
djongo.exceptions.SQLDecodeError:
Keyword: array
Sub SQL: ALTER TABLE "accounts_user" ADD COLUMN "conversations" array NULL
FAILED SQL: ('ALTER TABLE "accounts_user" ADD COLUMN "conversations" array NULL',)
Params: ([],)
Version: 1.3.4
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/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 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 231, in handle
post_migrate_state = executor.migrate(
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/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/akram/Desktop/cointracker/.venv/lib/python3.8/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/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 110, in database_forwards
schema_editor.add_field(
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 480, in add_field
self.execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/cursor.py", line 59, in execute
raise db_exe from e
django.db.utils.DatabaseError
注意:当我 运行 python manage.py migrate
时,创建迁移没有问题,但是当我应用它们时,出现上述错误
问题已解决!
原来这个文件 djongo/sql2mongo/query.py
中的 Djonogo 有问题
这个问题已经在 djongo Github 页面上的 comment 中得到了回答
我正在尝试将 ArrayField
存储在名为 User
的 Django 模型中,我正在使用 MongoDB 和 Djongo。
我设法用一个名为 Pair
的模型做到了,我可以存储 Pair
的列表并使用 ArrayField
将该列表存储在 User
模型中这相当简单,我只是从 Djongo 的官方文档
现在我正在尝试存储另一个列表,但出于某种原因,它一直给我一个巨大的 TraceBack(90 行)和一个不明确的错误:'django.db.utils.DatabaseError'
这是我的用户模型:
class User(AbstractBaseUser):
email = models.EmailField(verbose_name='email address', max_length=127, unique=True)
first_name = models.CharField(verbose_name='First Name', max_length=30, blank=True)
last_name = models.CharField(verbose_name='Last Name', max_length=30, blank=True)
is_active = models.BooleanField(default=True, verbose_name='Active')
is_admin = models.BooleanField(default=False, verbose_name='Admin')
pairs = models.ArrayField (
model_container=Pair,
null=True
)
conversations = models.ArrayField(
model_container=Conversation,
null=True
)
同样,pairs 属性工作正常,但对话不正常
Pair
模型
class Pair(models.Model):
name = models.CharField(max_length=16)
purchase_price = models.DecimalField(verbose_name='Purchase price', max_digits=20, decimal_places=6)
class Meta:
ordering = ['name']
abstract=True
Conversation
模型
class Conversation(models.Model):
handle = models.CharField(max_length=255)
class Meta:
abstract = True
这是追溯:
Operations to perform:
Apply all migrations: accounts, admin, auth, contenttypes, sessions, tracker
Running migrations:
Not implemented alter command for SQL ALTER TABLE "accounts_user" ADD COLUMN "conversations" array NULL
Applying accounts.0003_user_conversations...Traceback (most recent call last):
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/cursor.py", line 51, in execute
self.result = Query(
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 783, in __init__
self._query = self.parse()
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 875, in parse
raise e
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 856, in parse
return handler(self, statement)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 888, in _alter
query = AlterQuery(self.db, self.connection_properties, sm, self._params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 425, in __init__
super().__init__(*args)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 84, in __init__
super().__init__(*args)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 62, in __init__
self.parse()
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 435, in parse
self._add(statement)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 598, in _add
raise SQLDecodeError(err_key=tok.value,
djongo.exceptions.SQLDecodeError:
Keyword: array
Sub SQL: ALTER TABLE "accounts_user" ADD COLUMN "conversations" array NULL
FAILED SQL: ('ALTER TABLE "accounts_user" ADD COLUMN "conversations" array NULL',)
Params: ([],)
Version: 1.3.4
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/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 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 231, in handle
post_migrate_state = executor.migrate(
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/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/akram/Desktop/cointracker/.venv/lib/python3.8/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/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 110, in database_forwards
schema_editor.add_field(
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 480, in add_field
self.execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/cursor.py", line 59, in execute
raise db_exe from e
django.db.utils.DatabaseError
注意:当我 运行 python manage.py migrate
时,创建迁移没有问题,但是当我应用它们时,出现上述错误
问题已解决!
原来这个文件 djongo/sql2mongo/query.py
中的 Djonogo 有问题这个问题已经在 djongo Github 页面上的 comment 中得到了回答