I'm trying to store an ArrayField
in a Django model called User
, I'm using MongoDB and Djongo.
I managed to do it with one model called Pair
, I can store a list of Pair
s and store that list in a User
model using ArrayField
which was fairly simple, I just followed this example from Djongo's official doc
Now I'm trying to store another list but for some reason, it just keeps giving me a huge TraceBack (90 lines) with an ambiguous error: 'django.db.utils.DatabaseError'
here's my user model:
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
)
again, the pairs attribute is working just fine but the conversation is not
The Pair
model
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
The Conversation
model
class Conversation(models.Model):
handle = models.CharField(max_length=255)
class Meta:
abstract = True
and this is the traceback:
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
Note: when I run python manage.py migrate
, migrations are created with no problem, but when I apply them, I get the above error
Problem solved!
turned out that there's a problem with Djonogo specifically in this file djongo/sql2mongo/query.py
this question has already been answered in this comment on the djongo Github page