Home > other >  How GORM to create on the primary key value?
How GORM to create on the primary key value?

Time:10-26

 
Type MerchantApply struct {
Id int ` gorm: "-; Primary_key; AUTO_INCREMENT "`
Name, Address string
Referee_id int
CreatedAt time. Time
The State, Provider_id int
Contact, Tel, Remark string
}

/* *
Create business application information
*/
Func (merchants * merchants) CreateMerchantApply (killing MerchantApply) (error) {
Err:=Db. The Create (& amp; Killing). The Error
Return err
}

I need the new record id is returned to the front end, how do I get?

CodePudding user response:


After executing SQL running SELECT LAST_INSERT_ID (); Can be found out

CodePudding user response:


The type of Menu struct {
Id int ` gorm: "primary_key `
The Title string ` gorm: "type: varchar (100); Not null;" `
Pid int ` gorm: "type: int. Not null "`
Important int ` gorm: "type: int. Not null "`
PowerType int ` gorm: "type: int. Not null "`
The Type int ` gorm: "Type: tinyint; Not null "`
}

Func (Menu) TableName (string) {
Return "table"
}

Var db * gorm. Db


Func insert (menu * menu) (int, error) {
If the menu. The Title=="" {
Return 1, nil
}
Var err error
Dd:=db. The Create (menu)
Id:=menu. Id
//insert data
If err:=dd. The Error; Err! Nil={
FMT. Println (err)
Return 1, err
}
Return the id, err
}

CodePudding user response:

The menu=& amp; The Menu {
Id: 0,
Title: col4,
Pid: col3_id,
Important: 0,
PowerType: kk,
Type: 0,
}
Col4_id, err=insert (menu)
  • Related