question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IndexError: tuple index out of range when using db.execute_sql

See original GitHub issue

I have function to create on PostgreSQL:

jsonb_flatten_function = """
create or replace function create_jsonb_flat_view
    (view_name text, table_name text, regular_columns text, json_column text)
    returns text language plpgsql as $$
declare
    cols text;
begin
    execute format ($ex$
        select string_agg(format('%2$s->>%%1$L "%2$s.%%1$s"', key), ', ')
        from (
            select distinct key
            from %1$s, jsonb_each(%2$s)
            order by 1
            ) s;
        $ex$, table_name, json_column)
    into cols;
    execute format($ex$
        drop view if exists %1$s;
        create view %1$s as 
        select %3$s, %4$s from %2$s
        $ex$, view_name, table_name, regular_columns, cols);
    return cols;
end $$;
"""

If try to execute the previous sql with

db.execute_sql(jsonb_flatten_function)

it raises

Traceback (most recent call last):
  File "/home/user/init_pg.py", line 91, in <module>
    db.execute_sql(jsonb_flatten_function)
  File "/home/user/mainenv/lib/python3.6/site-packages/peewee.py", line 2940, in execute_sql
    cursor.execute(sql, params or ())
IndexError: tuple index out of range

However if I use a psycopg2 curosr manually or use:

cursor = db.cursor()
cursor.execute(jsonb_flatten_function)

It works without any issue.

Removing or () from cursor.execute(sql, params or ()) seems to fix it, I’m not sure why it exists in the first place though.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
TimothyBramlettcommented, Aug 14, 2019

@coleifer @ymrzkrrs Nevermind, simply included an extra % to escape the %:

and ((id %% 2)) = 0)
0reactions
TimothyBramlettcommented, Aug 14, 2019

@ymrzkrrs Did you ever solve this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Psycopg2 execute tuple index out of range - Stack Overflow
This is my first shot at using SQL as well as psycopg2. Any "dumb" advice is much appreciated! I am not sure what...
Read more >
Python IndexError: tuple index out of range Solution
The IndexError: tuple index out of range error occurs when you try to access an item in a tuple that does not exist....
Read more >
cursor.execute("SELECT '%' ") --> tuple index out of range
File "/localhome/modw/django/db/backends/util.py", line 21, in execute return self.cursor.execute(sql, params) IndexError: tuple index out of range.
Read more >
[Solved]: Python IndexError: tuple index out of range
The IndexError: tuple index out of range error occurs when you try to access an item in a tuple that does not exist....
Read more >
30408 (CheckConstraint with lookup using LIKE & % crash on ...
... File "/Users/davids/src/django/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) IndexError: tuple index out of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found