Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gf生成dao or model 时无法自动生成with 等关联属性 #1293

Closed
wordgao opened this issue Jun 17, 2021 · 3 comments
Closed

gf生成dao or model 时无法自动生成with 等关联属性 #1293

wordgao opened this issue Jun 17, 2021 · 3 comments

Comments

@wordgao
Copy link

wordgao commented Jun 17, 2021

使用gf gen dao 或 gf gen model 原生工具生成

自动生成的dao or model 无关联信息,也无法使用with等函数,是否是bug。
目前我需要设置商品+对应的阶梯单价
1对多模式, 需要的效果是 增删改查

产品信息

CREATE TABLE `tb_sku` (
  `id` int NOT NULL AUTO_INCREMENT,
  `create_time` datetime(6) NOT NULL,
  `update_time` datetime(6) NOT NULL,
  `DataCode` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `productName` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `stock` int DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
) ENGINE=InnoDB AUTO_INCREMENT=70062 DEFAULT CHARSET=utf8;

产品阶梯价格

CREATE TABLE `tb_price` (
  `id` int NOT NULL AUTO_INCREMENT,
  `Qty` int NOT NULL,
  `price` decimal(10,5) NOT NULL,
  `product_id` int DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  KEY `tb_price_product_id_27d09a1a_fk_tb_sku_id` (`product_id`) USING BTREE,
  CONSTRAINT `tb_price_product_id_27d09a1a_fk_tb_sku_id` FOREIGN KEY (`product_id`) REFERENCES `tb_sku` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB AUTO_INCREMENT=354510 DEFAULT CHARSET=utf8;
GoFrame CLI Tool v1.16.2, https://goframe.org
GoFrame Version: v1.16.2 in current go.mod
CLI Installed At: D:\GoProjects\bin\gf.exe
CLI Built Detail:
  Go Version:  go1.16.3
  GF Version:  v1.16.0
  Git Commit:  4c84e08c182c5df921880ba2c0429a3e813a3336
  Build Time:  2021-06-03 20:43:58

4. 这是gf gen dao 生成的结构

type priceColumns struct {
	Id        string //
	Qty       string //
	Price     string //
	ProductId string //
}

func NewPriceDao() *PriceDao {
	return &PriceDao{
		M:     g.DB("default").Model("tb_price").Safe(),
		DB:    g.DB("default"),
		Table: "tb_price",
		Columns: priceColumns{
			Id:        "id",
			Qty:       "Qty",
			Price:     "price",
			ProductId: "product_id",
		},
	}
}

这是gf gen model 生成的结构体信息

type Price struct {
	Id        int     `orm:"id,primary" json:"id"`        //
	Qty       int     `orm:"Qty"        json:"qty"`       //
	Price     float64 `orm:"price"      json:"price"`     //
	ProductId int     `orm:"product_id" json:"productId"` //
}

// Entity is the golang structure for table tb_price.
type Entity struct {
    Id        int     `orm:"id,primary" json:"id"`         //   
    Qty       int     `orm:"Qty"        json:"qty"`        //   
    Price     float64 `orm:"price"      json:"price"`      //   
    ProductId int     `orm:"product_id" json:"product_id"` //   
}
type Product struct {
	Sku *model.Sku
	PriceList []*model.Price
}
func main(){
	db := g.DB()
	// 演示数据信息
	var P Product
	err:=db.Table("tb_sku").Scan(&P.Sku,g.Map{"productModel":"apple", "SimpleCode":"appshoptag"})
	if err != nil {
		fmt.Println(err)
	}

	r,err:=db.Table("tb_price").Where("product_id", P.Sku.Id).FindAll()
	if err != nil {
		fmt.Println(err)
	}
	// 测试过Scan和ScanAll
	err := db.Table("tb_price").Scan(&P.PriceList, "product_id", P.Sku.Id)
	//改为dao模式去操作P.Sku.Id可能报错会提示找不到字段Id
	_=dao.Price.Scan(&P,"product_id", P.Sku.Id) //很奇怪的问题,有时候能找到,有时候找不到P.Sku.(Id)
}

我需要通过商品model+店铺字段查询 对应的唯一数据进行增删改查,请问如何能对关联的阶梯价格实现增删改查.
如果只是对商品信息增删改查比较容易通过dao或者是model实现,但现在的主要问题是关联的阶梯单价操作无法实现。

@arieslee
Copy link
Contributor

自行维护吧,官网文档上不是说了嘛!

goframe没有采用其他ORM常见的BelongsTo, HasOne, HasMany, ManyToMany这样的模型关联设计,这样的关联关系维护较繁琐,例如外键约束、额外的标签备注等,对开发者有一定的心智负担。

@gqcn
Copy link
Member

gqcn commented Jul 8, 2021

@wordgao @arieslee 我看你的表结构中存在外键关联,但是目前CLI工具无法根据外键关联生成with标签,这点是需要改进的。如果感兴趣也可以给CLI提PR。

@wordgao
Copy link
Author

wordgao commented Jul 8, 2021

好的

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Planned Issues
  
To do
Development

No branches or pull requests

3 participants