I'm trying to write a very simple belongsTo association with GORM, but with primary keys that isn't Id.
My structs are as such:
type State struct {
FIPS string `gorm:"type:char(2);primary_key;column:FIPS"`
Name string `gorm:"not null"`
Area float64 `gorm:"type:real;not null"`
}
type ZipCode struct {
ZipCode string `gorm:"type:char(5);primary_key;"`
Name string `gorm:"not null"`
State State `gorm:"ForeignKey:StateFIPS;AssociationForeignKey:FIPS"`
StateFIPS string `gorm:"type:char(2);column:state_FIPS;not null"`
}
and with the following code:
var zc ZipCode
var s State
db.Model(&zc).Related(&s)
I get the error: [2017-05-18 14:26:13] invalid association [] and a find on the zipcode doesn't load the state. Does GORM not like non-Id primary keys or am I missing something?
With your current code :
var zc ZipCode
var s State
db.Model(&zc).Related(&s)
You are not set anything to your zc variable. so that's why you get an error of invalid association [] with an empty data.
To fix this you must get the ZipCode data from your database like :
db.First(&zc, 1) // find ZipCode with id 1.
and then you can associate your zc full code would be :
var zc ZipCode
var s State
db.First(&zc, 1) // find ZipCode with id 1.
db.Model(&zc).Related(&s)
Note : I'm not testing this actual code but I think it would fix the problem.
Related
I have issue with my first GORM usage. I want to load Point with preloaded Team structure. Go throw error that int64 cannot be stored in models.Team. I suppose that I have to use GORM the wrong way, but I am not able to find out why. Database contain data, relation many2many on other structs works fine for me (users in Team struct).
type Point struct {
Id int `gorm:"primaryKey"`
TeamX Team `gorm:"column:team_id;foreignKey:id"` // <<< this struct I want to get
CreatedAt time.Time
Note string
}
func (Point) TableName() string {
return "points"
}
type Team struct {
Id int `gorm:"primaryKey"`
Name string
Users []User `gorm:"many2many:teams_users;"`
}
func (Team) TableName() string {
return "teams"
}
var obj models.Point
log.Println(getDatabase().Preload("Teams").First(&obj, 1).Error.Error())
fmt.Println(obj.Id) // <<< this one loads ok
fmt.Println(obj.TeamX.Id) // <<< this throw error
points table
id int(11), auto_increment
team_id int(11), foreign key teams(id)
createdAt datetime
note text
teams table
id int(11), auto_increment
name text
sql: Scan error on column index 1, name "team_id": unsupported Scan, storing driver.Value type int64 into type *models.Team
Suppose you have database models as follows:
package storage
type Country struct {
ID string `json:"id" gorm:"type:uuid"`
Name string `json:"name"`
Code string `json:"code"`
}
type City struct {
ID string `json:"id" gorm:"type:uuid"`
Name string `json:"name"`
Code *string `json:"code"`
CountryId string `json:"country_id"`
Country *Country `json:"country" gorm:"references:ID"`
IATA *string `json:"iata"`
Latitude *string `json:"latitude"`
Longitude *string `json:"longitude"`
}
The city should have a pointer to a Country model to make it easier to understand whether Country has been joined (in sql) or not (e.g. if city.Country == nil {panic("for whatever reason")} )
The problem appears when I try to get the list of all cities:
package example
var cities []storage.City
tx.Joins("Country").Find(&cities)
Here, all the cities have been fetched from DB nicely, but the countries became the same in all the cities.
EXPECTED OUTPUT
[
{
ID:51e415ab-4301-4268-9345-deed6b1d72f6
Name:Bergen
Code:0xc0004d6ec0
CountryId:0bd3890c-b6b7-4b27-8071-55c8f64562bb
Country:{
ID:0bd3890c-b6b7-4b27-8071-55c8f64562bb
Name:Norwegen
Code:NO
}
SkyScannerId:0xc0004d6ee0
IATA:0xc0004d6ef0
Latitude:0xc0004d6f00
Longitude:0xc0004d6f10
},
{
ID:2468c7f0-0275-4bff-8b7e-4e87bfa63604
Name:Banská Bystrica
Code:0xc0004d6bc0
CountryId:00ba76d3-9591-4d45-a39d-f554375d790f
Country: {
ID:00ba76d3-9591-4d45-a39d-f554375d790f
Name:Slovakei
Code:SK
}
SkyScannerId:<nil>
IATA:<nil>
Latitude:0xc0004d6c00
Longitude:0xc0004d6c10
},
{
ID:75501988-3c80-4ef9-8081-73d20cbcc29b
Name:Prag
Code:0xc0004d6a60
CountryId:f4e819b2-5c1a-43f9-bfa1-fe56b6ee173e
Country:{
ID:f4e819b2-5c1a-43f9-bfa1-fe56b6ee173e
Name:Tschechien
Code:CZ
}
SkyScannerId:0xc0004d6a90
IATA:0xc0004d6aa0
Latitude:0xc0004d6ac0
Longitude:0xc0004d6ad0
}
]
ACTUAL OUTPUT:
[
{
ID:51e415ab-4301-4268-9345-deed6b1d72f6
Name:Bergen
Code:0xc0004d6ec0
CountryId:0bd3890c-b6b7-4b27-8071-55c8f64562bb
Country:{
ID:f4e819b2-5c1a-43f9-bfa1-fe56b6ee173e
Name:Tschechien
Code:CZ
}
SkyScannerId:0xc0004d6ee0
IATA:0xc0004d6ef0
Latitude:0xc0004d6f00
Longitude:0xc0004d6f10
},
{
ID:2468c7f0-0275-4bff-8b7e-4e87bfa63604
Name:Banská Bystrica
Code:0xc0004d6bc0
CountryId:00ba76d3-9591-4d45-a39d-f554375d790f
Country:{
ID:f4e819b2-5c1a-43f9-bfa1-fe56b6ee173e
Name:Tschechien
Code:CZ
}
SkyScannerId:<nil>
IATA:<nil>
Latitude:0xc0004d6c00
Longitude:0xc0004d6c10
},
{
ID:75501988-3c80-4ef9-8081-73d20cbcc29b
Name:Prag
Code:0xc0004d6a60
CountryId:f4e819b2-5c1a-43f9-bfa1-fe56b6ee173e
Country:{
ID:f4e819b2-5c1a-43f9-bfa1-fe56b6ee173e
Name:Tschechien
Code:CZ
}
SkyScannerId:0xc0004d6a90
IATA:0xc0004d6aa0
Latitude:0xc0004d6ac0
Longitude:0xc0004d6ad0
}
]
Please pay attention to Country field of the outputs. In the ACTUAL OUTPUT all the cities have the same country. I think this has something to do with the pointer.
I got EXPECTED OUTPUT when I removed the pointer from Country (so *Country became Country without *). But I would like to get the same output with the pointer (*Country).
Also, please do not pay attention to the values printed out in other fields. My main focus is the Country field.
Any ideas how to fix it ?
P.S. I know that i can survive without using the pointers too in the Country field, but I just want to know if there are any possibilities to do that.
Try to put countryId as a pointer too.
Like this
CountryId *string `json:"country_id"`
Actually I tried to replicate the error with you code example, but it's working well.
I have noticed that this is the behavior with gorm-v1.23.6. Switching back to gorm-v1.23.5 solved the problem.
I am trying to create a struct field, and limit its values to a list of values i.e,
state =["locked", "unlocked"]
now in Django models we use the field choices i.e
class Book(models.Model):
LOCKED = 'LK'
UNLOCKED = 'UN'
STATE = [
('LK', 'Locked'),
('UL', 'Unlocked'),
]
book_state = models.CharField(choices=STATE, default=LOCKED)
trying to replicate the above using a gorm.model struct data type in Go.
Solution: create a custom golang type with string and add it as gorm model field
type BookState string
const (
Locked BookState = "locked"
Unlocked BookState = "unlocked"
)
Then create your gorm struct model fields
type Book struct {
Name string `json:"name" validate:"required"`
State BookState `json:"state" validate: "required"`
....
}
How to set unique at the struct specific columns. first name
type User struct {
ID int64 `orm:"size(100)", pk`
Lastname string `orm:"size(100)"`
Firstname string `orm:"size(100)"`
Role string `orm:"size(100)"`
Created time.Time `orm:"size(100)"`
Updated time.Time `orm:"size(100)"`
}
I'm using "github.com/astaxie/beego/orm"
According to the documentation, you just add the word "unique" to the tag:
Add unique key for one field
Name string `orm:"unique"`
To combine tags, you must use a semicolon as documented here. For example:
Firstname string orm:"unique;size(100)"
I'm playing a bit with Gorm while I'm trying to decide which ORM library fit the most for my needs.
Important to mention that I'm currently working with Sqlite.
Following the guide I created two structs:
type Color struct {
gorm.Model
UserID uint
Name string
}
//User struct define a basic user model
type User struct {
gorm.Model
Username string
Email string
FirstName string
LastName string
Password string
CreationDate time.Time
DOB time.Time
IgnoreMe int `gorm:"-"` // Ignore this field
Color Color `gorm:"foreignkey:ColorRefer"`
ColorRefer uint
}
when I'm creating a DB with
func CreateTables() {
user := dm.User{}
color := dm.Color{}
GormDB.CreateTable(&color)
GormDB.CreateTable(&user)
GormDB.Model(&user).AddForeignKey("ColorRefer", "colors(id)", "CASCADE", "CASCADE")
}
or with:
func CreateTables() {
GormDB.AutoMigrate(&dm.User{},&dm.Color{})
}
Sadly it's not working as I would of expect and create the foreign key automatically, but it's works when I do it manually.
My main problem is when I'm trying to query Users
//QueryByStructExample query users table by the struct non-zero (non-default) fields.
func QueryByStructExample(userStruct dm.User) []dm.User {
var results []dm.User
GormDB.Where(userStruct).Find(&results)
return results
}
I created the following function in a try to query users by email with the color property which is my color struct and I tried to play with a lot with the Model,Related and the Association functions, and nothing seems to work and (I'm avoiding to use join by purpose).
The end result is that it query my User but without the color (only with the ID in my colorRefer)
any suggestion?
Do you mean to preload the Color struct? If yes did you try to query it like that
GormDB.Preload('Color').Where(userStruct).Find(&results)