Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table Alias Names Exceeds 63 Bytes #1367

Open
l0gicgate opened this issue Aug 28, 2019 · 6 comments
Open

Table Alias Names Exceeds 63 Bytes #1367

l0gicgate opened this issue Aug 28, 2019 · 6 comments

Comments

@l0gicgate
Copy link

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?

@vmihailenco
Copy link
Member

vmihailenco commented Aug 29, 2019

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.

@CyborgMaster
Copy link

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

@l0gicgate
Copy link
Author

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

@CyborgMaster
Copy link

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

@rbroggi
Copy link

rbroggi commented Dec 31, 2022

same problem here 😞

@rbroggi
Copy link

rbroggi commented Jan 2, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants