You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've recently started using bun in a project at work in which I need to do some simple updates to several tables in the DB. The trouble is that the DB tables have inconsistent column names, like in the example below:
typeEnglishBookstruct {
bun.BaseModelIdint64Titlestring`bun:"book_title"`
}
// Spanish books were added later and the creator of that table thought it appropriate to use a different column nametypeSpanishBookstruct {
bun.BaseModelIdint64Titlestring`bun:"original_title"`
}
EnglishBook.Title column is called book_title, while titles for Spanish books are stored in the column original_title.
Now, imagine I wanted to update both tables and append ", Vol: 1" to the books' titles.
Currently it seems that I would have to write 2 different queries:
which feels somewhat unwieldy.
What I would like to do instead would be sth like this:
// Define a function that updates the column that corresponds to the struct's field `.Title`funcaddVolumeToTitle(modelinterface{}) {
db.NewUpdate().Model(model).Set("?Title = ?Title || ', Vol: 1')").Exec(ctx)
}
// Then simply call for both tablesaddVolumeToTitle((*EnglishBook)(nil))
addVolumeToTitle((*SpanishBook)(nil))
}
I went through the placeholders section, but it seems to only have placeholders for listing all columns (?Columns) or PK columns, which doesn't really solve my issue.
Is there anything I could use to achieve that? Or should I think of a custom solution?
Update: fieldByGoName() is the method that would apparently be used to deliver such functionality. However, at the moment it is only used to parse the "join" tag on tables with many-to-many relationship and not for usual queries. Is my understanding correct? If so, do you think it would make sense to allow such Go-name syntax in queries?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
bun bun everyone🥯
I've recently started using
bun
in a project at work in which I need to do some simple updates to several tables in the DB. The trouble is that the DB tables have inconsistent column names, like in the example below:EnglishBook.Title
column is calledbook_title
, while titles for Spanish books are stored in the columnoriginal_title
.Now, imagine I wanted to update both tables and append
", Vol: 1"
to the books' titles.Currently it seems that I would have to write 2 different queries:
which feels somewhat unwieldy.
What I would like to do instead would be sth like this:
I went through the placeholders section, but it seems to only have placeholders for listing all columns (
?Columns
) or PK columns, which doesn't really solve my issue.Is there anything I could use to achieve that? Or should I think of a custom solution?
Update:
fieldByGoName()
is the method that would apparently be used to deliver such functionality. However, at the moment it is only used to parse the"join"
tag on tables with many-to-many relationship and not for usual queries. Is my understanding correct? If so, do you think it would make sense to allow such Go-name syntax in queries?Beta Was this translation helpful? Give feedback.
All reactions