-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
If a column is called "group" (or other reserved words) the example link to the table doesn't work #134
Comments
Fix is to wrap the column name in double quotes: select id, name, notes, disabled, previous_names, slug, "group" from availability_tag Need a list of reserved SQL words - or some other mechanism for detecting them, maybe psycopg2 has something? |
https://www.psycopg.org/docs/extensions.html#psycopg2.extensions.quote_ident
|
That quotes identifiers whether or not they need to be quoted, which I find a little ugly. It gave me this result: select "id", "name", "notes", "disabled", "previous_names", "slug", "group" from availability_tag I'm optimistic about this to help solve the problem: select * from pg_get_keywords() |
It looks like the full list of reserved words (that are invalid if used as column names without double quotes) can be found with: select string_agg(word, ', ') from pg_get_keywords() where catcode = 'R' It's:
|
I could incorporate that into this SQL query in a clever way: django-sql-dashboard/django_sql_dashboard/views.py Lines 141 to 164 in dd1bb18
Or I could run it once at the start, pull the list of reserved words into Python and apply optional escaping in Python code. |
Doing it with fancy SQL looks like it could work really well: with visible_tables as (
select table_name
from information_schema.tables
where table_schema = 'public'
order by table_name
),
reserved_keywords as (
select word
from pg_get_keywords()
where catcode = 'R'
union select 'id' as word
)
select
information_schema.columns.table_name,
string_agg(column_name, ', ' order by ordinal_position) as columns,
array_agg(case when column_name in (select word from reserved_keywords) then '"' || column_name || '"' else column_name end order by ordinal_position) as columns
from
information_schema.columns
join
visible_tables on
information_schema.columns.table_name = visible_tables.table_name
where
information_schema.columns.table_schema = 'public'
group by
information_schema.columns.table_name
order by
information_schema.columns.table_name (I added the Output: |
I also need to solve this for "sort by column" queries, see #57 - so I'm going to load the results of |
Python 3.9 introduced a def cache(user_function, /):
'Simple lightweight unbounded cache. Sometimes called "memoize".'
return lru_cache(maxsize=None)(user_function) |
|
That won't work because the function needs to be passed a connection. I'll roll my own dumb caching mechanism instead. |
_reserved_words = None
def postgresql_reserved_words(connection):
global _reserved_words
if _reserved_words is None:
with connection.cursor() as cursor:
cursor.execute("select word from pg_get_keywords() where catcode = 'R'")
_reserved_words = [row[0] for row in cursor.fetchall()]
return _reserved_words |
Clicking this link:
Links to this SQL which throws an error:
Error:
The text was updated successfully, but these errors were encountered: