-
Hi @vmihailenco! It's possible to shorten table relation aliases? I'm facing an issue similar to #524. I have this example code (I've changed the table and struct names, because I can't share the project data): type TheNameOfStructInPrivCodebase struct {
bun.BaseModel `bun:"table:the_name_of_table_in_the_database,alias:osih"`
ID int64 `json:"id" bun:"id,pk,autoincrement"`
TheNameOfSecondTableID int64 `json:"the_name_of_second_table_id"`
CreatedAt time.Time `json:"created_at" bun:",nullzero,notnull,default:current_timestamp"`
UpdatedAt time.Time `json:"updated_at" bun:",nullzero,notnull,default:current_timestamp"`
// Relationships
TheNameOfSecondStruct *TheNameOfSecondStruct `json:"the_name_of_second_table" bun:"rel:belongs-to,join:the_name_of_second_table_id=id"`
}
type TheNameOfSecondStruct struct {
bun.BaseModel `bun:"table:the_name_of_second_table,alias:osi"`
ID int64 `json:"id" bun:"id,pk,autoincrement"`
TheNameOfThirdTableID int64 `json:"the_name_of_third_table_id"`
CreatedAt time.Time `json:"created_at" bun:",nullzero,notnull,default:current_timestamp"`
UpdatedAt time.Time `json:"updated_at" bun:",nullzero,notnull,default:current_timestamp"`
// Relationships
TheNameOfThirdStruct *TheNameOfThirdStruct `json:"the_name_of_third_table" bun:"rel:belongs-to,join:the_name_of_third_table_id=id"`
}
type TheNameOfThirdStruct struct {
bun.BaseModel `bun:"table:the_name_of_third_table,alias:oi"`
ID int64 `json:"id" bun:"id,pk,autoincrement"`
ThisInformationUUID string `json:"this_information_uuid"`
CreatedAt time.Time `json:"created_at" bun:",nullzero,notnull,default:current_timestamp"`
UpdatedAt time.Time `json:"updated_at" bun:",nullzero,notnull,default:current_timestamp"`
}
func GetData(id int64) (*TheNameOfStructInPrivCodebase, error) {
db := db.Conn()
db.AddQueryHook(bundebug.NewQueryHook())
m := new(TheNameOfStructInPrivCodebase)
err := db.NewSelect().
Model(m).
Relation("TheNameOfSecondStruct").
Relation("TheNameOfSecondStruct.TheNameOfThirdStruct").
Where("osih.id = ?", id).
Scan(context.TODO())
return m, err
} Calling After some research, I found about In my case, bun is generating an alias with 72 bytes: Then, by default, PostgreSQL slices the alias generating a different one: The problem is: bun uses this alias notation to Scan the rows into structs: After the slice, bun is searching for a field called There's a way to shorten the alias generated by bun? P.S.: I've tried using the
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I found a way to do this: type TheNameOfStructInPrivCodebase struct {
bun.BaseModel `bun:"table:the_name_of_table_in_the_database,alias:osih"`
ID int64 `json:"id" bun:"id,pk,autoincrement"`
TheNameOfSecondTableID int64 `json:"the_name_of_second_table_id"`
CreatedAt time.Time `json:"created_at" bun:",nullzero,notnull,default:current_timestamp"`
UpdatedAt time.Time `json:"updated_at" bun:",nullzero,notnull,default:current_timestamp"`
// Relationships
TheNameOfSecondStruct *TheNameOfSecondStruct `json:"the_name_of_second_table" bun:"osi,rel:belongs-to,join:the_name_of_second_table_id=id"`
}
type TheNameOfSecondStruct struct {
bun.BaseModel `bun:"table:the_name_of_second_table,alias:osi"`
ID int64 `json:"id" bun:"id,pk,autoincrement"`
TheNameOfThirdTableID int64 `json:"the_name_of_third_table_id"`
CreatedAt time.Time `json:"created_at" bun:",nullzero,notnull,default:current_timestamp"`
UpdatedAt time.Time `json:"updated_at" bun:",nullzero,notnull,default:current_timestamp"`
// Relationships
TheNameOfThirdStruct *TheNameOfThirdStruct `json:"the_name_of_third_table" bun:"oi,rel:belongs-to,join:the_name_of_third_table_id=id"`
}
type TheNameOfThirdStruct struct {
bun.BaseModel `bun:"table:the_name_of_third_table,alias:oi"`
ID int64 `json:"id" bun:"id,pk,autoincrement"`
ThisInformationUUID string `json:"this_information_uuid"`
CreatedAt time.Time `json:"created_at" bun:",nullzero,notnull,default:current_timestamp"`
UpdatedAt time.Time `json:"updated_at" bun:",nullzero,notnull,default:current_timestamp"`
}
func GetData(id int64) (*TheNameOfStructInPrivCodebase, error) {
db := db.Conn()
db.AddQueryHook(bundebug.NewQueryHook())
m := new(TheNameOfStructInPrivCodebase)
err := db.NewSelect().
Model(m).
Relation("TheNameOfSecondStruct").
Relation("TheNameOfSecondStruct.TheNameOfThirdStruct").
Where("osih.id = ?", id).
Scan(context.TODO())
return m, err
} Notice that in I don't know if it was a misunderstanding, but it wasn't clear to me that I should use a tag that changes a column name as an alias in a table relationship... It would be nice if bun used the alias already defined in the child structure ( Anyway, I'm going to mark this as solved, but I think it's a good discussion for an improvement. Wrapping up: if you want to use custom alias names in bun relationship queries, just set the |
Beta Was this translation helpful? Give feedback.
I found a way to do this: