13

I have class method in django model which is using json_array_elements function.

In case when it performs by browser it works fine. But in tests it fails. python manage.py test

Traceback (most recent call last):
  File "path_to_project/dj_server/model_animations/tests.py", line 94, in test_cteating
    response_first = model_animations.views.get_animations_list(request, groupid)
  File "path_to_project/dj_server/model_animations/views.py", line 37, in get_animations_list
    for model_anim in listArray:
  File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 1535, in __iter__
    query = iter(self.query)
  File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 76, in __iter__
    self._execute_query()
  File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 90, in _execute_query
    self.cursor.execute(self.sql, self.params)
  File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
ProgrammingError: function json_array_elements(text) does not exist
LINE 1: ...on_name FROM model_animations_model, json_array...
                                                             ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

in models.py

@classmethod
def animations_list(self, group_id):
    if group_id:
        try:
            listArray = ModelAnimationList.objects.raw(('SELECT * FROM model_animations_model, json_array_elements(animated_groups) AS data WHERE \'"%s"\' = data::text' %group_id))
            return listArray
        except:
            pass
    return None

in views.py def get_animations_list(request, group_id): ...

listArray = ModelAnimationList.animations_list(group_id)
if listArray:
    for model_anim in listArray:
        if model_anim:
            anim_dict = { 'a_id'  : model_anim.id, 'a_name' : model_anim.animation_name }
            result_anim_list.append(anim_dict)

...

in tests.py

request = HttpRequest()
response_first = model_animations.views.get_animations_list(request, groupid)

Installed:
python 2.7
Django 1.7.1
Postgres 9.3.5
psycopg2 2.5.4
Mac 10.10 yosemite

2
  • Could you please post the code for your failing test cases? Commented Nov 14, 2014 at 14:57
  • oh, it's a lot of code. I will try make something simple. Commented Nov 14, 2014 at 16:37

2 Answers 2

36

Mistake in query json_array_elements(animated_groups)

Needs change to this: json_array_elements(animated_groups::json)

Sign up to request clarification or add additional context in comments.

1 Comment

For me, I had to use to_jsonb. E.g. json_array_elements(to_jsonb(items))
10

Postgres JSON processing functions such as json_array_elements(animated_groups::json), jsonb_array_elements(animated_groups::jsonb) will take json or jsonb values.

Because postgres JSON field(animated_groups) are store as text format or binary format. so we need to convert the type by using either ::json or ::jsonb it give the json or jsonb format

check here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.