SQLite Forum

Parse error when a RETURNING clause refers to the alias of the inserted table
Login

Parse error when a RETURNING clause refers to the alias of the inserted table

(1.1) By Nicolas Évrard (nicoe_) on 2023-05-26 14:11:53 edited from 1.0 [source]

I'm using version 3.40.1 of SQLite and I noticed that there is a crash when the reurning clause is using the alias of the inserted table instead than the original name:

❯ sqlite3 :memory:
SQLite version 3.40.1 2022-12-28 14:03:47
Enter ".help" for usage hints.
sqlite> CREATE TABLE t1(a INT,b INT);
sqlite> INSERT INTO t1(a,b) VALUES(1,2) RETURNING t1.a;
1
sqlite> INSERT INTO t1 AS a (a,b) VALUES(1,2) RETURNING a.b;
Parse error: no such column: a.b
  INSERT INTO t1 AS a (a,b) VALUES(1,2) RETURNING a.b;
                                    error here ---^

(2) By Larry Brasfield (larrybr) on 2023-05-25 15:41:36 in reply to 1.0 [link] [source]

When I run that version of the CLI, I see exactly the same result from the same inputs. By "same", I mean the responses shown in the body of your post.

What I do not see is anything that would normally be called "Crash". Perhaps you believe that the term refers vaguely to any sort of result differing from the expected, but such belief does not reflect common usage. In common usage, in a software context, a "crash" is an abnormal program termination, not arising from normal execution of the program's code. An example is when a program accesses putative memory addresses which are not mapped to any actual memory, causing a hardware-level fault and interruption/termination of the program by the operating system.

I invite you to edit the title of your post to better reflect the facts.

If you are also seeing an actual crash (per the term's common meaning), please state what operating system and version you are using, and the nature of the crash as reported by the operating system as it terminates the program.

(4) By Nicolas Évrard (nicoe_) on 2023-05-26 14:15:10 in reply to 2 [link] [source]

Sorry to not use the right term, I was rather in a hurry.

And indeed it's not crash but rather a deviation from the documentation as the documentation states that the alias can be used in the RETURNING clause.

(5) By Adrian Ho (lexfiend) on 2023-05-26 15:23:39 in reply to 4 [link] [source]

the documentation states that the alias can be used in the RETURNING clause.

I'm curious, where does it state what you claim?

The only reference I can find that's even close to relevant is in the INSERT doc:

The optional "AS alias" phrase provides an alternative name for the table into which content is being inserted. The alias name can be used within WHERE and SET clauses of the UPSERT. If there is no upsert-clause, then the alias is pointless, but also harmless.

(7) By Nicolas Évrard (nicoe_) on 2023-05-27 08:44:24 in reply to 5 [link] [source]

I'm curious, where does it state what you claim?

In RETURNING when you expand expr it shows that schema_name.table_name.column_name can be used. I thought that it meant that people could use any name that would refer to table_name including the alias used in the query.

But OK I agree I had a misconception of SQLite behaviour probably based on the fact that it worked with postgres.

(8) By Cédric Krier (cedrickrier) on 2023-06-18 08:28:22 in reply to 5 [link] [source]

In RETURNING, it says that the clause is modeled after PostgreSQL. As PostgreSQL supports to use alias, one may expect to work also with SQLite. If it is not wanted, it may be included in the "Limitations And Caveats".

(3) By Paul Stuart (pstuart) on 2023-05-25 15:49:53 in reply to 1.0 [link] [source]

Try with a different alias, as the one you chose collides with a column name.

(9) By Cédric Krier (cedrickrier) on 2023-06-18 08:29:43 in reply to 3 [link] [source]

It does not change and normally the parser should expect a schema name (or alias) and not a column name.

(6) By Keith Medcalf (kmedcalf) on 2023-05-26 15:26:38 in reply to 1.1 [link] [source]

Why are you assigning an alias to the table name you are inserting into?

You are aware, are you not, that the RETURNING clause only returns values from the table being modified (insert/update/delete)?

I do believe the documentation states this fact, and that you are merely observing the documented behaviour in action.

(10) By Cédric Krier (cedrickrier) on 2023-06-18 08:31:53 in reply to 6 [link] [source]

Such queries are generated by an ORM that supports SQLite and PostgreSQL. As PostgreSQL supports to return more than just the columns from the table (with USING or CTE), an alias is assigned to prevent any ambiguity.