Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Ori Newman
@someone235
Hi, someone knows how to load a model instance with its relations with gorm? I didn't really understand how to do it from the docs
HyperLiar
@HyperLiar
Hi, someone knows how to build a sql with alias like 'select alias_a.elem, alias_b.elem from table_a as alias_a inner join table_a as alias_b on table_a.id = table_b.id where {condition_a} and {condition_b};', I need to select an interact group in the same table with different condition.
vinod023
@vinod023

Hi All, I am using method
if Err = DB.Model(testtable).Set("gorm:insert_option", fmt.Sprintf("ON CONFLICT (id) DO UPDATE SET name = '%v', voterid='%v',eventstarttime= '%v' ",
testtable.Name, testtable.VillageID, testtable.StartTime.Format(time.RFC3339Nano))).Create(&testtable).Error; Err != nil {
}

I got errors : pq: syntax error at or near "Connell"
pq: syntax error at or near "Mara"

How can I resolve this?

Christophe de Vienne
@cdevienne
quit
FuncGuy
@FuncGuy
Hi

package main

import "testing"

func TestVerifierRecievesInput(t *testing.T) {
handler, out := setup()

handler.Listen()

if recieved := <-out; recieved != 1 {
    t.Errorf("\n Got:  %v\nWant: 1", recieved)
}

}

func setup() (handler *VerifyHandler, out chan interface{}) {
in := make(chan interface{}, 10)
out = make(chan interface{}, 10)
handler = NewVerifyHandler(in, out)
in <- 1
close(in)
return handler, out
}

when i execute i'm getting compiler error saying ./verify_handler_test.go:17:24: undefined: VerifyHandler
please help on this
FuncGuy
@FuncGuy
i am pretty new to golang
FuncGuy
@FuncGuy
got it :)
DIY
Ori Newman
@someone235
Hi, I want to find instances of a model with a has_one field, and to make a condition on the has_one field. For example, fetch all of the articles that their writer is Asian.
Is it possible?
Ian Chenyang Zhao
@IanZhao
I'm curious whether there's a way to use protobuf with goorm. Such the models' struct are defined with protobuf and compatible with goorm?
Stephanie Sunshine
@StephanieSunshine
Is it possible to call stored functions from gorm?
Gopher1980
@gopher1980
HI
I need your opinion of the my little module go for create CRUD on GORM
https://github.com/gopher1980/gormcrud
If you want help me, I will thank
Krew
@t2krew
how can i create table with suffix?
i have one data model, and want to create 16 tables with different suffix. show how can i do it use gorm?
Gopher1980
@gopher1980
You can change name your table implementation the method Tablename
func (user) TableName() string {
return "suffix_user"
}
I hope that my tip working for you
Krew
@t2krew
thanks @gopher1980
but my problem is , i have a model like type Record struct{}
i want to create tables more than one , just like record_0, record_1 ....
show i want to know how can i do this is graceful.
Gopher1980
@gopher1980
I think it is big problem
if you succeed mapping I don’t know how Gorm will query in the 16 tables for find data
Krew
@t2krew
yeah, i also want to how can i query data
Gopher1980
@gopher1980
I would map each table
Each entity I would do composite of the Record
I would create one method for query in all tables. But I think by chat is complicate help you
Krew
@t2krew
yes, but it looks not gracefully?
Gopher1980
@gopher1980
In my country is 3.04 am but if tomorrow i have time I will try example of my idea and I putting in one gist ;)
Krew
@t2krew
wow, you are so late to sleep. thanks @gopher1980
Gopher1980
@gopher1980
@krew do you try map to one view (select of all tables by union)
if you no insert and and no update this can good idea.
Krew
@t2krew
no, because in our business logic, a specical user's data should only store in one special table, just like we use the userid 's md5 value first character to be the table suffix. so if i want to query a special user's data, i just need to query that table (tablename_suffix).
not need to use 'union'
Gopher1980
@gopher1980
Ok I understand. Tomorrow I will try I make other solution. This problem is transform in personal jejej
Bye
Gopher1980
@gopher1980

@t2krew th idea is use TableName
var suffix string
type Record struct {
ID uint gorm:"primary_key" json:"id"
CreatedAt time.Time json:"created_at"
UpdatedAt time.Time json:"updated_at"
DeletedAt *time.Time json:"deleted_at"
Name string json:"name"
}
func (e Record) TableName() string {
return "record_" + suffix
}

.....

suffix = "1"
db.AutoMigrate(&Record{})
suffix = "2"
db.AutoMigrate(&Record{})
suffix = "1"
db.Save(&Record{Name: "data 1"})
suffix = "2"
db.Save(&Record{Name: "data 2"})

Krew
@t2krew
@gopher1980 thanks
YuShuangqi
@ysqi
hi,all, how to create a select with subquery .like select * from (select id from table)a
Gopher1980
@gopher1980
@StephanieSunshine jinzhu/gorm#1945
Rohit Roy Chowdhury
@roychowdhuryrohit-dev
Is it necessary to check db.DB().ping() before every db operation ? My each Insert requests is spending 1-2secs.
Rohit Roy Chowdhury
@roychowdhuryrohit-dev
Does GORM handle db update for multi nested jsonb type fields in postgres ? Update keys is exists else create them ?
vinod023
@vinod023

Hi, In my task, the data came from some where. per second 30 to 50 records came. So that I need to insert those records in DB before check those records presence in db or not. Some cases Find method throws an error like "record not found" if data presence in db. Whenever that error came, Create function was executed. In that time I am getting the error "pq: duplicate key value violates unique constraint "market_pkey"". I tried with db.Exec(), DB.Raw() and all other methods, but no solution. Any one can help me please?

case "data":
mar := v.(uimodels.Mar) //Here v is the interface{}
if Err = DB.Where("id = ?", ID).Find(&uimodels.Mar{}).Error; Err != nil {
if Err == gorm.ErrRecordNotFound {
DB.Create(&mar)
}
}

Where DB connections:
dbURI := fmt.Sprintf("dbname=%s host=%s port=%s user=%s password=%s sslmode=disable",
dbname, dbhost, dbport, dbuser, dbpass)

DB, err := gorm.Open("postgres", dbURI)

if err != nil {
    log.Fatal("Could not connect database")
}
DB.DB().SetMaxIdleConns(2)
DB.DB().SetMaxOpenConns(100)
DB.DB().SetConnMaxLifetime(5 * time.Minute)
Krew
@t2krew
type Record struct {
    ID       int64  `gorm:"Column:id"`
    ActId    string `gorm:"Column:actid"`
    Gid      string `gorm:"Column:gid"`
    UserID   int    `gorm:"Column:userid"`
    AddTime  string `gorm:"Column:addtime"`
}

func (t GiftRecord) TableName() string {
    return "records_" + utils.Md5(t.Actid)
}
var records []Record 

db.Where("actid=?", "helloworld").Find(&records)
I want to query multiple rows, how can I get the correctly table name?