如何在范围内插入记录数组
How to insert the array of records inside the range
如何在范围内插入结构?
//Department array (department table)
var depValues []model.Department = []model.Department{
model.Department{DepartmentName: "Software Analyst", DeptCode: "SA"},
model.Department{DepartmentName: "Business Analyst", DeptCode: "BA"},
}
for _, dep := range depValues {
db.Create(&dep) //It's working fine
}
这里,dep.ID
return两个记录id
//Another array (employee table)
var empValues []model.Employee = []model.Employee{
model.Employee{EmpId: "001", EmpName: "samsung", departmentRef: dep.ID},
model.Employee{EmpId: "002", EmpName: "Nokia", departmentRef: dep.ID},
}
现在我想把departmentId
插入employee
table,请问如何实现,谁教教我
已经尝试过
它将是 return 个重复值
var depValues []model.Department = []model.Department{
model.Department{DepartmentName: "Software Analyst", DeptCode: "SA"},
model.Department{DepartmentName: "Business Analyst", DeptCode: "BA"},
}
for _, dep := range depValues {
db.Create(&dep)
//Insert into Employee
var empValues []model.Employee = []model.Employee{
model.Employee{EmpId: "001", EmpName: "samsung", DepRefer: dep.ID},
model.Employee{EmpId: "002", EmpName: "Nokia", DepRefer: dep.ID},
}
for _, empv := range empValues {
db.Create(&empv)
}
}
数据库重复记录
'1', '2018-07-10 15:12:27', '2018-07-10 15:12:27', NULL, '001', 'samsung', '1'
'2', '2018-07-10 15:12:28', '2018-07-10 15:12:28', NULL, '002', 'Nokia', '2'
'3', '2018-07-10 15:12:29', '2018-07-10 15:12:29', NULL, '001', 'samsung', '1'
'4', '2018-07-10 15:12:29', '2018-07-10 15:12:29', NULL, '002', 'Nokia', '2'
如果我理解正确你的问题,你有两个变量,它们都是 slice.You 想要将其中一个附加到另一个:
这是您的代码和结果:
func main() {
var depValues = []Department{
{DepartmentName: "Software Analyst", DeptCode: "SA"},
{DepartmentName: "Business Analyst", DeptCode: "BA"},
}
for _, dep := range depValues {
fmt.Println(dep)
var empValues = []Employee{
{EmpId: "001", EmpName: "samsung", DepRefer: 1},
{EmpId: "002", EmpName: "Nokia", DepRefer: 2},
}
for _, empv := range empValues {
fmt.Println(empv)
}
}
}
你的结果
{Software Analyst SA []}
{001 samsung 1}
{002 Nokia 2}
{Business Analyst BA []}
{001 samsung 1}
{002 Nokia 2}
这是我的代码和结果
type Department struct {
DepartmentName string
DeptCode string
Employee []Employee
}
type Employee struct {
EmpId string
EmpName string
DepRefer int
}
func main() {
var depValues = []Department{
{DepartmentName: "Software Analyst", DeptCode: "SA"},
{DepartmentName: "Business Analyst", DeptCode: "BA"},
}
var empValues = []Employee{
{EmpId: "001", EmpName: "samsung", DepRefer: 1},
{EmpId: "002", EmpName: "Nokia", DepRefer: 2},
}
for _, dep := range depValues {
dep.Employee = empValues
fmt.Println(dep)
}
}
结果:
{Software Analyst SA [{001 samsung 1} {002 Nokia 2}]}
{Business Analyst BA [{001 samsung 1} {002 Nokia 2}]}
如何在范围内插入结构?
//Department array (department table)
var depValues []model.Department = []model.Department{
model.Department{DepartmentName: "Software Analyst", DeptCode: "SA"},
model.Department{DepartmentName: "Business Analyst", DeptCode: "BA"},
}
for _, dep := range depValues {
db.Create(&dep) //It's working fine
}
这里,dep.ID
return两个记录id
//Another array (employee table)
var empValues []model.Employee = []model.Employee{
model.Employee{EmpId: "001", EmpName: "samsung", departmentRef: dep.ID},
model.Employee{EmpId: "002", EmpName: "Nokia", departmentRef: dep.ID},
}
现在我想把departmentId
插入employee
table,请问如何实现,谁教教我
已经尝试过
它将是 return 个重复值
var depValues []model.Department = []model.Department{
model.Department{DepartmentName: "Software Analyst", DeptCode: "SA"},
model.Department{DepartmentName: "Business Analyst", DeptCode: "BA"},
}
for _, dep := range depValues {
db.Create(&dep)
//Insert into Employee
var empValues []model.Employee = []model.Employee{
model.Employee{EmpId: "001", EmpName: "samsung", DepRefer: dep.ID},
model.Employee{EmpId: "002", EmpName: "Nokia", DepRefer: dep.ID},
}
for _, empv := range empValues {
db.Create(&empv)
}
}
数据库重复记录
'1', '2018-07-10 15:12:27', '2018-07-10 15:12:27', NULL, '001', 'samsung', '1'
'2', '2018-07-10 15:12:28', '2018-07-10 15:12:28', NULL, '002', 'Nokia', '2'
'3', '2018-07-10 15:12:29', '2018-07-10 15:12:29', NULL, '001', 'samsung', '1'
'4', '2018-07-10 15:12:29', '2018-07-10 15:12:29', NULL, '002', 'Nokia', '2'
如果我理解正确你的问题,你有两个变量,它们都是 slice.You 想要将其中一个附加到另一个:
这是您的代码和结果:
func main() {
var depValues = []Department{
{DepartmentName: "Software Analyst", DeptCode: "SA"},
{DepartmentName: "Business Analyst", DeptCode: "BA"},
}
for _, dep := range depValues {
fmt.Println(dep)
var empValues = []Employee{
{EmpId: "001", EmpName: "samsung", DepRefer: 1},
{EmpId: "002", EmpName: "Nokia", DepRefer: 2},
}
for _, empv := range empValues {
fmt.Println(empv)
}
}
}
你的结果
{Software Analyst SA []}
{001 samsung 1}
{002 Nokia 2}
{Business Analyst BA []}
{001 samsung 1}
{002 Nokia 2}
这是我的代码和结果
type Department struct {
DepartmentName string
DeptCode string
Employee []Employee
}
type Employee struct {
EmpId string
EmpName string
DepRefer int
}
func main() {
var depValues = []Department{
{DepartmentName: "Software Analyst", DeptCode: "SA"},
{DepartmentName: "Business Analyst", DeptCode: "BA"},
}
var empValues = []Employee{
{EmpId: "001", EmpName: "samsung", DepRefer: 1},
{EmpId: "002", EmpName: "Nokia", DepRefer: 2},
}
for _, dep := range depValues {
dep.Employee = empValues
fmt.Println(dep)
}
}
结果:
{Software Analyst SA [{001 samsung 1} {002 Nokia 2}]}
{Business Analyst BA [{001 samsung 1} {002 Nokia 2}]}