1,976
questions
0
votes
0
answers
33
views
how to preload models that are not directly related
i have models like this
type TreatmentFormOutput struct {
ID uint
OutputID uint
Output Output
TreatmentFormID uint
Indicators []OutputIndicator ...
0
votes
0
answers
167
views
When I updated gorm version to v1.25.12, run dbresolver plugin (v1.5.2) failed
My code as follows
gormDb, err = gorm.Open(mysql.Open(fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", viper.GetString("mysql.username"), viper....
1
vote
0
answers
42
views
gorm: migrate from string to pq.StringArray doesn't work
I have now:
type tableA struct {
fieldA string
}
and I want to change from a single text to an array of text:
type tableA struct {
fieldA pq.StringArray `gorm:"type:text[]"`
}
but ...
0
votes
0
answers
19
views
How to dynamically route traffic to different databases?
dsn := "user:pass@tcp(127.0.0.1:3306)/testDB?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("...
0
votes
1
answer
27
views
GORM is not returning correct time.Time value for as Datetime type mysql column
Database is created by db.AutoMigrate(&models.User{})
User is in package: models
type User struct {
Username string `json:"username" gorm:"primaryKey; not null; ...
1
vote
0
answers
42
views
GORM - How to use a custom interface?
package models
import (
"gorm.io/gorm"
)
type GormQueryBuilder interface {
FindQueryBuilder(db *gorm.DB) *gorm.DB
}
type BaseInterface interface {
Get(GormQueryBuilder, *gorm....
1
vote
2
answers
43
views
Go GORM NOT IN query list of ids
I'm trying to run a SELECT * WHERE ID NOT IN (?) query in GORM and it does not seem to work.
The argument for the ? parameter comes from the application and is not a database subquery which would be ...
0
votes
0
answers
30
views
GORM Soft Delete - Also set UpdatedAt to match DeletedAt
We are using GORM soft delete behavior and we also want the UpdatedAt field set to match the DeletedAt field on soft delete, but it doesn't seem to do that.
Is there a way to configure it to do so, or,...
1
vote
1
answer
42
views
How can identifiers like roles and tables be used dynamically in GORM with Raw SQL?
I am working on a Go project using GORM and PostgreSQL, and I need to dynamically use identifiers such as roles and tables in my SQL queries. However, I am encountering an issue with parameterized ...
0
votes
0
answers
39
views
Why is GORM migration inferring the wrong data types for a many to many relationship?
I have the following structs generated by Gorm Gen:
type UserBase struct {
ID int32 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
Name string `...
0
votes
0
answers
78
views
How do you generate Gorm tables with relations?
I'm trying to achieve this
Add functional tests to my Go project
The tests should bring up an actual MySQL database (by connecting to a docker container already up and running from the docker ...
1
vote
1
answer
41
views
How do I omit fields completely when selecting in GORM?
I have my GORM Schemas shown below. Two tables i.e Items and Users. Every item is has a user who created the item.
type Item struct {
ID uuid.UUID `gorm:"type:uuid;default:...
0
votes
1
answer
61
views
Question about error raw query string using gorm
I generate raw query like that
UPDATE
public.openedu_user_tokens AS t
SET
update_at = c.update_at,
delete_at = c.delete_at,
user_id = c.user_id,
org_id = c.org_id,
email = c.email,
...
1
vote
2
answers
86
views
Golang gorm runtime error: invalid memory address appears after a long idle time when using same gorm.db connection
After some hours of running without problems I get the following runtime error:
[ERROR] runtime error: invalid memory address or nil pointer dereference
goroutine 37 [running]:
runtime/debug.Stack()
...
1
vote
1
answer
110
views
Why GORM automigrate will try to drop a non-exist index
I have a type as
type Customer struct {
gorm.Model
CorpId string `gorm:"column:corpid"`
Name string `gorm:"column:Name"`
Age int
}
And I use GORM to do the ...
2
votes
0
answers
69
views
GORM adding "AND column.id = 1", why?
Im having an issue with GORM on my GIN project.
Please help me if possible.
My issue is:
var class models.Classrom
old_classID = "algorithm"
DB.Model(&models.Classrom{}).Where("...
1
vote
1
answer
50
views
How to config query timeout default for all query?
I am working with GORM in my Go application and I need to set a default query timeout for all queries. Currently, I have individual queries where I set the timeout explicitly, But I want to avoid ...
0
votes
0
answers
12
views
Workbench not opening on the Desktop
I have installed Mysql on my system using the normal documentation procedures and instructions. I have also downloaded the Mysql workbench also by following instruction from Msql documentation website....
0
votes
0
answers
46
views
How to Write Unit Tests for GORM Repository Method with gomock?
I am trying to write unit tests for a GORM repository method using gomock to mock database interactions. However, I'm running into issues with nil pointer dereference errors. Below are the relevant ...
0
votes
1
answer
66
views
How to create a generic array in GO?
I have a function that needs to receive a GORM model as a parameter, and depending on the model I receive as a parameter, I need to create an Array(Slice) based on the type of the struct(gorm model) I ...
1
vote
1
answer
71
views
GORM Help w/ Postgres: unsupported data type [closed]
I'm trying to read through the CVE List to put all the entries into a central database. When I try to create an entry to this databse, I get an error: 2024/07/02 14:03:13 /x/db.go:68 unsupported data
...
-1
votes
1
answer
56
views
Cannot use exported method that belongs to the package in Golang
I've got a package named services. This is the structure of the package:
services/
third_party/
discogs.go
discogsAPIStructs.go
artist_srv.go
auth_srv.go
record_srv.go
...
-4
votes
1
answer
42
views
How to use subquery into field on gorm query
I have a query like this:
select a.id,
(
select max(b.last_update_login)
from login_history b
where b.user_id = a.id
) last_update_login
from user a
How do I convert this into a ...
1
vote
0
answers
59
views
Golang GORM Recursive Structure Migration (MySQL)
I'm trying to create a recursive table structure, but I have not had any luck. Here's a simple example of what I'm trying to achieve:
package models
import (
"gorm.io/gorm"
)
type ...
-2
votes
1
answer
376
views
dial tcp 192.168.48.2:3306: connect: connection refused
My connection to the DB keeps getting refused i tried to make sure all the ports were correct and parameters were matched up but i still keep getting this error
this is my docker compose file
...
0
votes
0
answers
44
views
GORM many2many Association Replace not working as expected
I have a GO application using GIN and GORM. I want to create a Roles + Capabilities database. A role can have multiple capabilities and a capability can be in multiple roles. I created the following ...
1
vote
0
answers
101
views
How to deploy a SQLite Database with a GO application
I have an application structured more or less like the following :
app
api
database
db.go
main.go
data
database
data.db
ui
I would like to connect as a ...
1
vote
0
answers
52
views
Select Data from Two Tables without a Join (Cartesian Product) in Gorm
I'm trying to select data from two SQL tables without a join. Essentially, I need a Cartesian Product of the two tables. The SQL statement I expect to use is: SELECT * FROM table1, table2 WHERE table1....
1
vote
0
answers
47
views
Run `go-mssqldb` and GORM at the same time
I have the legacy code that written with RAW query that already contains 700+ unique queries. The system is keep growing, but I feel writing RAW query everytime through database/sql are killing our ...
2
votes
0
answers
100
views
Use dynamic table name in Join with Gorm to add WITH(NOLOCK)
I'm using gorm in my golang project to connect a SQL Server database and everything works fine.
Now I would like to dynamically set the table name in my Joins() calls, like I can do with the main (...
1
vote
1
answer
34
views
mysql data cannot be queried using go gorm
I use gorm to count qps of mysql, but the query data is always empty.
The program did not report any errors, and I could have used similar code to get my own test table data.
My code
type mysqlQueires ...
1
vote
0
answers
34
views
Generic function to upsert objects using gorm in Go for mssql?
I'm attempting to create a generic function in Go for upserting objects in a Microsoft SQL Azure database using gorm, but am not too familiar with the details in the documentation for gorm and wanted ...
2
votes
1
answer
75
views
field define a valid foreign key for relations or implement the Valuer/Scanner interface
I have got this error . when I try to query
define a valid foreign key for relations or implement the Valuer/Scanner interface
This is my model struct.
type SkuDetails struct {
SkuID string ...
2
votes
1
answer
159
views
GORM enum type for postgres
I use GORM to work with PostgreSQL.
For example I have a such struct with custom enum type:
type MonitoringDatabase struct {
Name string `gorm:"primaryKey"`
DBType ...
1
vote
0
answers
92
views
Restore and update soft deleted record Go Gorm
We are creating a translation app with a golang and gorm backend
This is our POSTGRES database table:
DataBase Table screenshot
This is our Go Struct:
type TermLocale struct {
models.Model
...
0
votes
0
answers
45
views
gorm define a valid foreign key for relations
I have following tables in postgresql:
CREATE TABLE IF NOT EXISTS "offices" (
"id" uuid NOT NULL DEFAULT uuid_generate_v4(),
"full_name" ...
1
vote
1
answer
38
views
Gorm append in association mode fails my validation checks even when both the objects exist
I am using the GORM ORM library for Golang to model the above relation. Here is the model code for the same:
package models
import (
"errors"
"fmt"
"regexp"
...
1
vote
0
answers
60
views
Optional relations in database model in GORM
Using a custom DbAuthoring model I add fields createdBy to my database models. This works fine (I have to populate them manually fetching the user ID out of the JWT claim though).
Of course, the IDs ...
1
vote
2
answers
51
views
how to use gorm to make a raw query to DB and get fields from different tables in result?
I have the following (note that in the query I expect to get fields from different tables. Maybe there is some kind of peculiarity in this):
type Result struct {
ID string `gorm:"...
1
vote
0
answers
69
views
Failed Catching Error Record Not Found with GORM
I tried to select a record with the code below.
err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("description = ?", item.Description).First(&exist).Error
...
1
vote
0
answers
23
views
How can I delete associated models in Gorm?
I'm facing a problem with deleting ModelA along with its associated ModelB instances using Gorm's soft delete methods. Here's a simplified version of my code:
type ModelA struct {
gorm.Model
...
1
vote
1
answer
143
views
Testing with gorm and sqlmock: could not match query with expected regexp
I am working on a project using GORM and I am trying to write tests using SQLMock. Below is the code snippet defining my custom types and a database model using GORM:
// ActionType action type
type ...
1
vote
1
answer
74
views
Traversing many-to-many relationship in condition using gorm
I use a postgres database and naming strategy
schema.NamingStrategy{
NoLowerCase: true,
SingularTable: true,
}
and have a database model like this:
type Project struct {
gorm.Model ...
0
votes
1
answer
162
views
Failed to initialize database, got error dial tcp 192.168.16.2:3306: connect: connection refused
Got [error] failed to initialize database, got error dial tcp 192.168.16.2:3306: connect: connection refused when run docker compose up -d
docker-compose.yml
version: "3"
services:
app:
...
1
vote
0
answers
62
views
MySQL Cloud - Golang (Random invalid transaction; invalid transaction; invalid transaction Error)
I don't know what the culprit is in this issue, the first time I run this REST API it works fine then I run it the 2nd time and I get this error "Invalid Transaction, Invalid Transaction, ...&...
1
vote
0
answers
48
views
Fetch Previous State In Gorm V2 Callback
I am trying to query for the previous state of the row in a callback.
I've registered the callback as such:
callback.Update().Before("gorm:save_before_associations").Replace("...
2
votes
1
answer
87
views
Upsert Postgres JSONB column using GORM
There is a table in Postgres
Id
Code
Data (jsonb)
1
xc1
{"name": "xx", "instance":"test_instance", "system": "test_system"}
2
cc3
{"...
1
vote
1
answer
38
views
GORM creating/inserting a model with a foreign key field returns an error (cannot convert)
here are two models I have and postgres migrations for these two models (every separate migration and struct is in its separate file):
type BaseModel struct {
ID uint64 `gorm:"...
1
vote
1
answer
58
views
GORM many to many with addition field as primary key in join table
Is it possible to add an additional field to the composite primary key created on the join table of a "many to many" relationship?
The reason is for allowing multiple identical records (that ...
1
vote
1
answer
79
views
GORM fails to query boolean json field in MySQL
NOTE: I have only tested this issue with MySQL.
When querying a property inside a json field with a bool argument, the query returns 0 rows. However, if you embed the bool into the where clause itself,...