djongo icon indicating copy to clipboard operation
djongo copied to clipboard

SQLDecodeError when using ArrayModelField

Open danielgmartins opened this issue 8 years ago • 1 comments

Django v.2.0.3 MongoDB v.3.6

I'm sorry for the mix of Portuguese with English in my code. But most things are in English so it should still be understandable. In any case, I included some translations.
Also, I'm quite new to both Django and Djongo, so please bare with me.

One line description of the issue

I can't seem to be able (or know how) to insert data, using the Admin page, when the model uses ArrayModelField

Python script

I have an app, and inside that app called logs;

  • on the models.py file:
from djongo import models
from django import forms

# For non Portuguese speakers:
# "campo" translates to "field"
# "teste" translates to "test"
# "lista_campos" translates to "field_list"

class Teste(models.Model):
    campo = models.CharField( max_length= 50 )
    class Meta:
      abstract = True

class TesteForm(forms.ModelForm):
    class Meta:
      model = Teste
      fields = ( 'campo', )
        
class TesteSubmission( models.Model ):
    username = models.CharField( max_length = 50 )
    code = models.TextField( )
    date = models.DateTimeField( auto_now = True )
    lista_campos = models.ArrayModelField(  # Field I'm concerned with
        model_container=Teste,
        model_form_class=TesteForm,
        null=True,
      )
  • on the admin.py file:
from django.contrib import admin
from .models import * 

admin.site.register(ViewExercise)

Now, when I want to enter new a TesteSubmission on the admin page I get this form: TesteSubmission Form in admin site Now, right away, my question is: why do I have the option to add only one field for campo? On the docs, with the Author list inside the Entry model, it shows at least two possible entries.

Even so, when I try to add an entry, I get: Error

Note: I was did this with brand new models, no make sure it wasn't an issue with previous data on the database. But I'll probably have to purge all the data in the database eventually.

Traceback

 Internal Server Error: /admin/logs/testesubmission/add/
 Traceback (most recent call last):
   File "/usr/local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 495, in parse
     return handler(self, statement)
   File "/usr/local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 644, in _insert
     self._query = InsertQuery(self, self.db, self.connection_properties, sm, self._params)
   File "/usr/local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 324, in __init__
     super().__init__(*args)
   File "/usr/local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 72, in __init__
     self.parse()
   File "/usr/local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 337, in parse
     raise MigrationError(collection)
 djongo.sql2mongo.MigrationError: logs_testesubmission

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

 Traceback (most recent call last):
   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
     response = get_response(request)
   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
     response = self.process_exception_by_middleware(e, request)
   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
     response = wrapped_callback(request, *callback_args, **callback_kwargs)
   File "/usr/local/lib/python3.6/site-packages/django/contrib/admin/options.py", line 574, in wrapper
     return self.admin_site.admin_view(view)(*args, **kwargs)
   File "/usr/local/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
     response = view_func(request, *args, **kwargs)
   File "/usr/local/lib/python3.6/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
     response = view_func(request, *args, **kwargs)
   File "/usr/local/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 223, in inner
     return view(request, *args, **kwargs)
   File "/usr/local/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1553, in add_view
     return self.changeform_view(request, None, form_url, extra_context)
   File "/usr/local/lib/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper
     return bound_func(*args, **kwargs)
   File "/usr/local/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
     response = view_func(request, *args, **kwargs)
   File "/usr/local/lib/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func
     return func.__get__(self, type(self))(*args2, **kwargs2)
   File "/usr/local/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1450, in changeform_view
     return self._changeform_view(request, object_id, form_url, extra_context)
   File "/usr/local/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1490, in _changeform_view
     self.save_model(request, new_object, form, not add)
   File "/usr/local/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1026, in save_model
     obj.save()
   File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 729, in save
     force_update=force_update, update_fields=update_fields)
   File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 759, in save_base
     updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
   File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 842, in _save_table
     result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
   File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 880, in _do_insert
     using=using, raw=raw)
   File "/usr/local/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
     return getattr(self.get_queryset(), name)(*args, **kwargs)
   File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 1125, in _insert
     return query.get_compiler(using=using).execute_sql(return_id)
   File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1283, in execute_sql
     cursor.execute(sql, params)
   File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
     return super().execute(sql, params)
   File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
   File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
     return executor(sql, params, many, context)
   File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
     return self.cursor.execute(sql, params)
   File "/usr/local/lib/python3.6/site-packages/djongo/cursor.py", line 53, in execute
     params)
   File "/usr/local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 435, in __init__
     self.parse()
   File "/usr/local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 512, in parse
     raise exe from e
 djongo.sql2mongo.SQLDecodeError: FAILED SQL: INSERT INTO "logs_testesubmission" ("username", "code", "date", "lista_campos") VALUES (%(0)s, %(1)s, %(2)s, %(3)s)
 Version: 1.2.22
 [15/Mar/2018 15:05:16] "POST /admin/logs/testesubmission/add/ HTTP/1.1" 500 204296

Update: Just noticed that this issue also happens if I replace ArrayModelField with just an EmbeddedModelField.

danielgmartins avatar Mar 15 '18 15:03 danielgmartins

HI @danielgmartins from the traceback, it looks like you have missed running migrations somewhere, which is resulting in this issue. Please try setting up your project as described in the documentation

nesdis avatar Mar 18 '18 03:03 nesdis