How to specify a struct with a multi-column unique index for Gorm? - go

How do I define my structs to specify a multi-column unique index to Gorm in Go?
Such as:
type Something struct {
gorm.Model
First string `sql:"unique_index:unique_index_with_second"`
Second string `sql:"unique_index:unique_index_with_first"`
}

this is how you do it: You need to use the gorm struct tag and specify that the index is unique
type Something struct {
gorm.Model
First string `gorm:"index:idx_name,unique"`
Second string `gorm:"index:idx_name,unique"`
}

You can define same unique index for each column.
type Something struct {
gorm.Model
First string `sql:"unique_index:idx_first_second"`
Second string `sql:"unique_index:idx_first_second"`
}

for latest version of gorm (or for my case)
this works:
type Something struct {
gorm.Model
First string `gorm:"uniqueIndex:idx_first_second"`
Second string `gorm:"uniqueIndex:idx_first_second"`
}

Related

Gorm w/ Postgres has a nested struct as JSON

I am using GORM and I want to store the following struct in Postgres:
// Value GoDoc
type Value struct {
Name string
Place string
Value interface{}
}
// SharedConfig stores tenant-wide shared values
type SharedConfig struct {
gorm.Model
TenantID string
ProgramID *string
Name string
Value Value
}
I want to store SharedConfig in my DB and want the Value column to be stored as JSON, is this possible? I've only seen uses of the json column type as postgres.Jsonb

Specify key for many-to-many relationship in go-pg ORM

I have these 2 models with many-to-many relationship:
type Person struct {
tableName struct{} `sql:"person"`
UUID string `sql:"person_uuid,pk"`
ContactDatas []ContactData `pg:",many2many:person_contact_data,joinFK:"`
}
type ContactData struct {
tableName struct{} `sql:"contact_data"`
UUID string `sql:"contact_data_uuid,pk"`
}
And model for person_contact_data table is:
type PersonContactData struct {
tableName struct{} `sql:"person_contact_data"`
PersonUUID string `sql:"person_uuid"`
ContactDataUUID string `sql:"contact_data_uuid"`
}
If joinFK in ContactDatas struct tag is empty go-pg adds underscore under the hood, so generated SQL part looks like this: WHERE ("contact_data"."contact_data_uuid" = person_contact_data."_contact_data_uuid").
Is there a way to specify joining keys completely manual?
I was using version 5. In latest version this was fixed, now you can specify full joining keys:
type Person struct {
tableName struct{} `sql:"person"`
UUID string `sql:"person_uuid,pk"`
ContactDatas []ContactData `pg:",many2many:person_contact_data,fk:person_uuid,joinFK:contact_data_uuid"`
}

Golang Decode a BSON with special character Keys to a struct

I have a Golang struct called Person where all the properties have to be exported:
type Person struct {
Id string
Name string
}
Now I need to encode my MongoDB BSON response to this Person struct. The BSON looks like:
{
"_id": "ajshJSH78N",
"Name": "Athavan Kanapuli"
}
The Golang code to encode the BSON is:
mongoRecord := Person{}
c := response.session.DB("mydb").C("users")
err := c.Find(bson.M{"username": Credentials.Username, "password": Credentials.Password}).One(&mongoRecord)
The Problem:
_id is not getting encoded into Id
If I change the Person property into _Id, then it won't be exported.
How can I solve this problem?
Define your struct with json tag-
type Person struct {
Id string `json:"_id"`
Name string // this field match with json, so mapping not need
}
I tried to put a json tag like ,
type Person struct {
Id string `json:"_id"`
Name string // this field match with json, so mapping not need
}
But still it didn't work. Because the Mongodb returns '_id' which is of type bson.ObjectId . Hence changing the Struct tag to bson:"_id" and the type of the Person struct has been changed from string to bson.ObjectId. The changes done are as follows ,
type Person struct {
Id bson.ObjectId `bson:"_id"`
Name string
UserName string
IsAdmin bool
IsApprover bool
}
And It works!

structs with multiple mapping notations

I have these two structs which represent the same entities (one comes from a Json file and the other from a DB)
type DriverJson struct {
ID int `json:"id"`
Name string `json:"name"`
}
type DriverOrm struct {
ID int `orm:"column(id);auto"`
Name string `orm:"column(name);size(255);null"`
}
I want to merge them into one Driver struct, how do I merge the mapping notations (orm:, json:)?
Thank you
As mentioned in the documentation of reflect.StructTag, by convention the value of a tag string is a space-separated key:"value" pairs, so simply:
type DriverJson struct {
ID int `json:"id" orm:"column(id);auto"`
Name string `json:"name" orm:"column(name);size(255);null`
}
For details, see What are the use(s) for tags in Go?

golang initilize inner struct object in nested structs

I have a nested struct
type Posts struct {
Id int
CreatedAt time.Time
Data struct {
Title string
UserName string
}
}
I want to create a Data object but var innerData Posts.Data doesn't seem to work. I don't want to create separate Data struct because I'm planning to avoid name collusions by having multiple structs which will have different inner structs named Data.
You can't. Posts.Data isn't the name of a type. The only thing you could do is var innerData struct{ ... }, which I'm sure you don't want to because then you would have repetition and need to make changes in two places. Otherwise, you have to give it a name. That name doesn't have to be Data, it can be PostData or something to make it unique.

Resources