Table Alias Names Exceeds 63 Bytes
So there seems to be a problem with long column names when doing joins as the library aliases column names in this fashion:
entity__related_entity__aliased_column
I was getting the following error:
pg: can't find column=long_entity_name__related_entity__long_column_nam in model=RelatedEntity (try discard_unknown_columns)
I realized that as you can see the column name gets cut off because Postgres's max identifier length is 63 bytes which the alias exceeds.
So when the results are returned, the library calls the function ScanColumn passing in a partial column name (cut off after 63 bytes):
https://github.com/go-pg/pg/blob/master/messages.go#L798
That results in an error. The discard_unknown_columns flag seems like a hackish way to circumvent the problem. Is there not a different way to approach table name aliasing to ensure we don't exceed 63 byte identifiers?
For now your only option is to use shorter aliases and shorter column names. As a general fix we could use some hashing to shorter generated column names, but I don't know when I will have time to work on that.
@l0gicgate, any luck figuring this out? We're running into the same thing.
@CyborgMaster the only solution is to make the name of your entities less descriptive (shorter in length) and hope for the best unfortunately. This naming strategy was supposed to be fixed a while ago but doesn't seem like it has been.
Thanks for the update. Much appreciated. Maybe I'll try to whip together a patch.
same problem here 😞
Dear @vmihailenco ,
For now your only option is to use shorter aliases and shorter column names. As a general fix we could use some hashing to shorter generated column names, but I don't know when I will have time to work on that.
Is there a way to provide an alias to a relation that is different than the go field?
example:
type User struct {
ID int64 `pg:",pk"`
ProfileID int64 `pg:",fk:Profile"`
Profile *Profile `pg:"rel:has-one"`
}
type Profile struct {
ID int64 `pg:",pk"`
UserID int64 `pg:",fk:User"`
}
In order to avoid long aliases while querying a User by it's PK and using the Relation("Profile") method, I would like to have an alias for the profiles table that is different than the name of the field Profile but I cannot find a way to achieve that without changing the name of the field.
Is my questions even clear? 😄