sqlize icon indicating copy to clipboard operation
sqlize copied to clipboard

It seems that sql statements cannot be generated properly when using gorm and the data table relationship is a many-to-many association.

Open liushuai05 opened this issue 2 years ago • 10 comments

It seems that sql statements cannot be generated properly when using gorm and the data table relationship is a many-to-many association.

liushuai05 avatar Aug 21 '23 09:08 liushuai05

Could you show me your code?

sunary avatar Aug 22 '23 04:08 sunary

Could you show me your code?

// Designer 案例分类表
type Designer struct {
	ID         uint64 `gorm:"primaryKey;column:id;type:bigint(20);AUTO_INCREMENT" json:"id"`
	Classify          []*DesignerClassify `gorm:"many2many:classify_to_designers" json:"classify"`         // 关联分类
}

// DesignerClassify 案例分类表
type DesignerClassify struct {
	ID         uint64 `gorm:"primaryKey;column:id;type:bigint(20);AUTO_INCREMENT" json:"id"`
	Title             string `gorm:"column:title;type:varchar(50)" json:"title"` // 公司名称
}

func (DesignerClassify) TableName() string {
	return "designer_classifys"
}
newMigration := sqlize.NewSqlize(sqlize.WithSqlTag("gorm"), sqlize.WithPluralTableName(), sqlize.WithMigrationFolder(""))
	var Designer = &models.Designer{}
	Designer.Classify = append(Designer.Classify, &models.DesignerClassify{})

	_ = newMigration.FromObjects(*Designer)
	stringUp := newMigration.StringUp()
	fmt.Println(111)
	fmt.Println(stringUp)
	fmt.Println(222)

image

Of course, I run these lines of code based on the artisoan of the goravel project, and I'm pretty sure it works, but I can't generate sql statements when associating queries (as if all associated queries are like this, and table structures without associations are normal).

liushuai05 avatar Aug 24 '23 02:08 liushuai05

Could you show me your code?

I looked at your source code and found that manually parsing the association didn't seem to work. In addition, I forgot to convert foreignKey to foreign_key automatically in the last pull request. I don't know if my guess is accurate.

image

liushuai05 avatar Aug 24 '23 03:08 liushuai05

Currently, sqlize doesn't support foreign keys because I'm still considering the convention. Perhaps I will follow the convention used by GORM: https://gorm.io/docs/has_many.html

Please wait for the next pull request. Thanks.

sunary avatar Aug 24 '23 03:08 sunary

Currently, sqlize doesn't support foreign keys because I'm still considering the convention. Perhaps I will follow the convention used by GORM: https://gorm.io/docs/has_many.html

Please wait for the next pull request. Thanks.

Thank you very much for bringing this issue to the author's attention. As my ability is limited, I can't give you much help. I'm very sorry about that. I will wait patiently for your update.

liushuai05 avatar Aug 24 '23 03:08 liushuai05

@liushuai05, I have added support for foreign key generation. Please feel free to reopen this if you encounter any issues.

sunary avatar Aug 29 '23 03:08 sunary

@liushuai05, I have added support for foreign key generation. Please feel free to reopen this if you encounter any issues.

I have tried to use it very well, but there is still a problem. The table sql statement generated when many-to-many tables are associated is empty. Theoretically, the table structure of the current model and the intermediate table should be generated.

liushuai05 avatar Sep 12 '23 02:09 liushuai05

@liushuai05, I have added support for foreign key generation. Please feel free to reopen this if you encounter any issues.

Can you set a conditional statement for foreign keys similar to sql_builder.WithGenerateForeignFey (), and generate foreign key statements only when it is true, otherwise no external sql statements will be generated, but other field sql statements will still be generated normally.

liushuai05 avatar Sep 12 '23 02:09 liushuai05

@liushuai05 could you show me your input and expectation?

sunary avatar Sep 13 '23 09:09 sunary

@liushuai05 could you show me your input and expectation?

1.There are some one-to-one and one-to-many projects in our company that are not strongly associated but are implemented in the golang layer and do not need to be added to the mysql database. Therefore, we need to turn on whether to generate foreign key statements. When this switch is turned on, a foreign key association statement is generated

2.When a many-to-many table association is encountered, the generated sql statement is empty, if you can generate the current table and the associated intermediate table sql statement, it is of course better, if not, then generate the table sql that initiates the association model The problem now is that the sql statements generated by the following model are empty. We want to generate sql statements at least for the Designer model, and it would be better if we could generate statements for classification association tables

// Designer 案例分类表
type Designer struct {
	ID         uint64 `gorm:"primaryKey;column:id;type:bigint(20);AUTO_INCREMENT" json:"id"`
	Classify          []*DesignerClassify `gorm:"many2many:classify_to_designers" json:"classify"`         // 关联分类
}

I'm really sorry, my English is so poor, I don't know if my description is completely accurate, the above content is from the translation software

liushuai05 avatar Sep 13 '23 09:09 liushuai05