pg icon indicating copy to clipboard operation
pg copied to clipboard

Table Alias Names Exceeds 63 Bytes

Open l0gicgate opened this issue 6 years ago • 6 comments

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?

l0gicgate avatar Aug 28 '19 17:08 l0gicgate

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.

vmihailenco avatar Aug 29 '19 13:08 vmihailenco

@l0gicgate, any luck figuring this out? We're running into the same thing.

CyborgMaster avatar Nov 09 '21 21:11 CyborgMaster

@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.

l0gicgate avatar Nov 10 '21 19:11 l0gicgate

Thanks for the update. Much appreciated. Maybe I'll try to whip together a patch.

CyborgMaster avatar Nov 10 '21 20:11 CyborgMaster

same problem here 😞

rbroggi avatar Dec 31 '22 15:12 rbroggi

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? 😄

rbroggi avatar Jan 02 '23 10:01 rbroggi