GORM Playground Link
https://github.com/go-gorm/playground/pull/815
Description
Context
In Postgres, query column identifiers are subject to Postgres' 63 character limit. While Postgres will accept queries with identifiers that are longer than this limit, the query response will truncate the identifier to the 63 character limit.
This has an impact on Gorm when it attempts to map a query response back into a struct.
Failing Use Case -- As Demonstrated in Playground Test Case
I have added a test case with a doubly nested entity. The test case expects the international_code
field to be populated following the join query, but it is ""
. This is a silent error.
This happens because the generated SQL query to retrieve this column is:
`CurrentEmployerInformation__PreferredCompanyLanguage`.`international_code` AS `CurrentEmployerInformation__PreferredCompanyLanguage__international_code`
CurrentEmployerInformation__PreferredCompanyLanguage__international_code
is larger than 63 characters long.
Postgres accepts this in the query correctly, but returns a truncated identifier for this column that is 63 characters long. Because of the truncation, Gorm cannot map the data to its intended column, leaving it empty.
Note that this happens silently; other columns are still populated. Using the NamingStrategy
with max length = 63 does not affect the identifier length in query building, and thus does not address this issue.
Comment From: maxwey
I tried to find prior art/related fixes, and found one relevant issue: PR 6337 Though it appears that it was meant to fix schema migration names rather than query generation.