我想预加载与gorm的关系,它没有用函数填充切片。M2MPreload
这是 sql 架构
create table project (
id int generated by default as identity,
name varchar(64) not null,
slug varchar(64) not null,
image_url text,
constraint project_pk primary key (id),
constraint project_unq unique (name, slug)
);
create table donation (
id int generated by default as identity,
user_id varchar(36) not null,
total_amount numeric(7, 2) not null,
currency varchar(3) not null,
constraint donation_pk primary key (id)
);
create table donation_detail (
donation_id int not null,
project_id int not null,
amount numeric(7, 2) not null,
primary key (donation_id, project_id)
);
这些是我的血腥模型
type Donation struct {
ID uint64 `json:"id" gorm:"primarykey"`
UserID string `json:"user_id"`
PaypalOrderID string `json:"paypal_order_id"`
TotalAmount float64 `json:"total_amount"`
Currency string `json:"currency"`
DonationDetails []*DonationDetail `json:"donation_details" gorm:"many2many:donation_detail;"`
}
type Project struct {
ID uint64 `json:"id" gorm:"primarykey"`
Name string `json:"name"`
Slug string `json:"slug"`
ImageURL string `json:"image_url"`
}
type DonationDetail struct {
DonationID uint64 `json:"donation_id" gorm:"primaryKey"`
ProjectID uint64 `json:"project_id" gorm:"primaryKey"`
Amount float64 `json:"amount"`
Project Project
}
我想退还捐款,它的详细信息包括项目信息。像这样:
{
"id": 1,
"user_id": "e14a98d1-0c4a-45c3-b748-5ac6ba733b99",
"paypal_order_id": "6SR91505YA360210R",
"total_amount": 10000,
"currency": "EUR",
"donation_details": [
{
"donation_id": 1,
"project_id": 1,
"amount": 3000,
"project": {
"project_id": 1,
"name": "First Project",
"slug": "first_project",
"image_url": "img1.jpg"
}
},
阿波罗的战车
相关分类