See https://github.com/jinzhu/gorm/commit/21f4de584f1cd93b15bf2ed498e861933ff37ff4

This is wrong and Postgres 9.3 will report a syntax error. The field names should not be prefixed with tablename.. The name resolution scoping is implied by the INSERT statement's table_name.

[2014-12-17 15:52:18] pq: syntax error at or near "."

Comment From: galeone

I'm currently using PostgreSQL 9.3 and all the queries works fine. What's your generated sql? Please post it.

By the way, the syntax is correct, since the query:

gorm=# insert into animals(counter,name,"from",age) values(2,'test',2,now()) returning animals.counter;
 counter 
---------
       2

as you can see, works without any problem

Comment From: DylanLukes

Here's an example:

This does not work:

INSERT INTO "user" ("created_at","deleted_at","reviewed_at","country","phone","email","username","updated_at","requested_at","display","given","family","city","status","expires_at","division","postal_code","password","line1","line2") 
VALUES ('2014-12-17 15:52:18.591497734 -0500','0001-01-01 00:00:00 +0000','0001-01-01 00:00:00 +0000','USA','redacted','email@site','username','2014-12-17 15:52:18.591497734 -0500','0001-01-01 00:00:00 +0000','Dylan Lukes','Dylan','Lukes','redacted ','0','0001-01-01 00:00:00 +0000','PA','----','','redacted','') 
RETURNING user.id
ERROR:  syntax error at or near "."
LINE 1: ...,'PA','ZIP','','','redacted') RETURNING user.id
                                                                    ^
********** Error **********

ERROR: syntax error at or near "."
SQL state: 42601
Character: 586

This does:

INSERT INTO "user" ("created_at","deleted_at","reviewed_at","country","phone","email","username","updated_at","requested_at","display","given","family","city","status","expires_at","division","postal_code","password","line1","line2") 
VALUES ('2014-12-17 15:52:18.591497734 -0500','0001-01-01 00:00:00 +0000','0001-01-01 00:00:00 +0000','USA','4122535870','lukes.dylan@gmail.com','dlukes','2014-12-17 15:52:18.591497734 -0500','0001-01-01 00:00:00 +0000','Dylan Lukes','Dylan','Lukes','Pittsburgh','0','0001-01-01 00:00:00 +0000','PA','15232','','5403 Friendship Ave','') 
RETURNING id
Total query runtime: 16 ms.
1 row retrieved.

Comment From: DylanLukes

From the documentation:

[ WITH [ RECURSIVE ] with_query [, ...] ]
INSERT INTO table_name [ ( column_name [, ...] ) ]
    { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
    [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
table_name

    The name (optionally schema-qualified) of an existing table.

...

output_expression

    An expression to be computed and returned by the INSERT command after each row is inserted. 
    The expression can use any column names of the table named by table_name. 
    Write * to return all columns of the inserted row(s).

Note that output_expression makes no mention of schema-qualification.

"the syntax is correct, since the query works" is incorrect reasoning. The spec is the word of truth on the matter and it states which field names are valid and how they should be formatted very clearly.

Comment From: galeone

user is a special role in postgres schema. Try to change the sql RETURNING user.id to

RETURNING "user".id

If the query works (and I think it does) I'll add as soon as possible the tablename between quotation marks

Comment From: DylanLukes

Yes! That does work.

How silly of me to not check for reserved keys/roles :). :+1:

Nonetheless, I'd be very wary of using syntax the documentation/syntax spec does not explicitly condone. It's generally pretty specific about what is allowed so I'm still hesitant considering Postgres could very well change this behavior.

Edit: honestly, nearly every single name output should be quoted, to avoid edge cases like this...

Comment From: galeone

I think the same. However using tablename.field is always right in sql. Thus don't bother about that.

I just make a pull request fixing this issue. Thank you