IndexError: tuple index out of range when using db.execute_sql
See original GitHub issueI 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:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@coleifer @ymrzkrrs Nevermind, simply included an extra % to escape the %:
@ymrzkrrs Did you ever solve this issue?