1,990 questions
1
vote
0
answers
99
views
Heavy duplication when upserting with GORM generics in SQLite
I have several models in my application, which have associations between each other. I'm trying to recursively upsert them, but haven't been able to find a good solution that doesn't extensively rely ...
0
votes
0
answers
38
views
Struggling with GO GORM v2's explicit column naming instead of struct fields
I learned about gorm just a few days ago and wanted to start using it for my next project.
But now I'm struggeling a bit with this:
if err = gorm.G[model.User](app.DB, clause.OnConflict{
...
0
votes
1
answer
60
views
How to get second relation data from the model in gorm
i am new to go-lang and i have been using gin, gorm and postgres for backend api and i want to get the data from the model.
user model:
package models
import "gorm.io/gorm"
// enum role
...
-3
votes
2
answers
102
views
PostgreSql Database Design [closed]
While designing social media application, i want my my users to have unique email and unique username, but the problem is, I am providing an otp verification and my table have coulmn id int primary ...
1
vote
1
answer
153
views
How to create dynamic GORM query with Join avoiding SQL injection?
I'm trying to create query helpers for my repository pattern. Actually I'm dealing how to add join clauses. The only idea is to inject Sprintf statement but this behavior might cause SQL injection.
...
0
votes
1
answer
127
views
GORM load associations from existing object
I know I can preload associations like this:
type User struct {
Name string
Documents []Document
}
type Document struct {
Name string
}
func loadUser() {
var u User
db.Preload("...
0
votes
0
answers
36
views
Why does this GORM UPSERT query fail on SQLITE but work on POSTGRES?
The below queries implements an UPSERT and works perfectly on POSTGRES
if err := query.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "finding_id"}, {Name: "...
0
votes
1
answer
38
views
query tables and conditions not cleared between different calls
I'm upgrading from gorm v1 to v2 and is totally confused about how I'm supposed to start a new query fresh. In v1, I can simply do db.Where("id=?", id).First(&user), which would result ...
0
votes
1
answer
79
views
Why do I get an error when creating records from a map with a PK field in GORM, and is this a valid use case?
I’ve been learning GORM from the official docs, and I’m running into some confusion when trying to create records from a map using the Create() function.
The official documentation shows this example ...
2
votes
2
answers
257
views
Is there a way to get the full number of results using GORM?
I'm currently writing a query with GORM, and I need to display a pagination information with the results.
In order to generate the pagination, I need the number of results that my query would return ...
1
vote
1
answer
57
views
How to check if table have column id ERROR: record "new" has no field "id"
I want to create an audit log table and attach trigger for the table to tables that have an id. When I insert record into an association table which doesn't have id, it triggers the following error ...
1
vote
2
answers
109
views
Json column insert into mysql db using GORM
I have two structs in Go that look like the following:
type struct UserInfo{
Name string `json:"name"`
Age int `json:"age"`
}
type struct Profile{
ID string `json:"...
0
votes
0
answers
22
views
Cannot serialise map using serialiser:json
I have a struct containing a map:
type Config struct {
ID int `gorm:"primarykey"`
Class templates.Class
Type string
Data map[string]any `gorm:"value,type:string,...
1
vote
1
answer
155
views
Does gorm auto-index created_at and updated_at columns when gorm.Model is embedded?
I am using gorm as the ORM in my Go application. I am embedding gorm.Model in all my model structs. For some reason, I assumed that created_at and updated_at columns will be auto-indexed because date ...
2
votes
1
answer
207
views
How can I use the gorm library to bulk upsert records into a postgres DB and return the IDs of those that are inserts and not updates?
Although this can be done with a raw query, I would like to bulk insert a small number of records and handle upsert conflicts through the gorm Create API. I need to return the IDs of records that are ...