Skip to content
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

PostgreSQL: Insert statement generation breaks for tables that have a column with the same name as the table #2217

Open
homedirectory opened this issue Mar 25, 2024 · 0 comments · May be fixed by #2225

Comments

@homedirectory
Copy link
Contributor

Description

It has been observed that the generation of INSERT statements fails under certain circumstances when using PostgreSQL.
In particular, when it is used with a table that has a column with the same name as the table.
The observation was made in the context of a TG entity PayrollCode which has property payrollCode. Incidentally, both the entity and the property had been mapped to the same name on the DB level: payrollcode_.

Invocation of the INSERT generating procedure produced the following error:

[322] ERROR:  malformed record literal: "Placeholder"
[322] DETAIL:  Missing left parenthesis.
[322] CONTEXT:  SQL statement "SELECT format('%L', ((($1)::payrollcode_)._id))"
        PL/pgSQL function create_insert_statement(regclass,anyelement) line 24 at EXECUTE

In general, an SQL statement invoking the generation of INSERTs look like so:

SELECT create_insert_statements(tableoid, mytable) FROM mytable;

Function create_insert_statements is called with 2 arguments:

  1. tableoid -- a special value from the system column tableoid of mytable
  2. mytable -- context-dependent:
    a. if mytable has a column named mytable, then that column will be used
    b. otherwise, the table itself will be used (this is the desired outcome)

It can be seen that the meaning of the 2nd argument is context-sensitive, leading to an undesired
ambiguity. Fortunately, the ambiguity can be avoided by explicitly referring to the table in
question, which can be accomplished in several ways:

  1. aliasing
SELECT create_insert_statements(tableoid, t) FROM mytable as t;

This has the disadvantage that additional effort is required to ensure the name used for the alias
is unique.

  1. mytable.*

From the PostgreSQL documentation (SELECT List):

... you can write table_name.* as a shorthand for the columns coming from just that table.

SELECT create_insert_statements(tableoid, mytable.*) FROM mytable;

Expected outcome

Generation of insert statements does not break due to the described edge case.

@homedirectory homedirectory self-assigned this Mar 25, 2024
@homedirectory homedirectory changed the title PostgreSQL: Insert statement generation breaks for tables that have a column with the same as the table PostgreSQL: Insert statement generation breaks for tables that have a column with the same name as the table Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant