SQLite User Forum

No such column when using schema in returning clause
Login

No such column when using schema in returning clause

(1) By Emile Fugulin (sytten) on 2022-05-05 19:03:52 [link] [source]

Hi,

There seems to be a limitation not listed in the documentation when using the schema name in a RETURNING.

Consider the following example:

CREATE TABLE test (
  id integer,
  name string
);
INSERT INTO test VALUES (1, 'testing') RETURNING main.test.id;

This currently returns an error: no such column: main.test.id despite being a valid expression. This is not a big deal when using the main schema but it can become an important limitation when attaching another database.

Many ORM always put the full name of the column in select and returning statements since that is a behaviour supported in postgres. For example, the rust ORM Diesel has this issue.

Unless there is specific reason for not supporting it, I think it would be better to align the behaviour with postgres and support the schema name in the expression (and update the documentation limitations in the meantime).

Thanks

(2) By Richard Hipp (drh) on 2022-05-05 20:33:07 in reply to 1 [link] [source]

The RETURNING clause is only able to return columns from the table that is being inserted into. So the "main.test." part of the column name is pointless. Just use "id". That should be completely unambiguous.

(3.1) By Emile Fugulin (sytten) on 2022-05-05 23:34:37 edited from 3.0 in reply to 2 [link] [source]

Like I mentioned in the post, enabling this feature is primarily to play nice with ORMs that most developers use so they don't have to write an exception for SQlite and they can basically reuse the code that they already have for Postgres.

I can also reverse the argument and say why support table.column at all as this is also "useless" currently.

(4) By anonymous on 2022-05-06 14:57:04 in reply to 3.1 [source]

Since PostgreSQL also doesn't need the qualification with schema and table name, it would be wise for all tools to not include these into the statement.

Since there is no returning clause in the standard as it is am idea of the PostgreSQL developers, it's no violation of the syntax of any dbms.

(5) By Emile Fugulin (sytten) on 2022-05-06 17:39:54 in reply to 4 [link] [source]

This is only true if you return directly from the same table.
Postgres supports much more advanced returning statement thus ORMs usually must specify the table because it is often not possible for them to know if there will be a name conflict.