I'm trying to achieve this
Add functional tests to my Go project
The tests should bring up an actual MySQL database (by connecting to a docker container already up and running from the docker compose config that runs the tests).
The test should create all tables, add some test data, run the tests and then delete everything.
I want my generated ORM to use the Gorm hints on the table fields.
I can currently use Gorm Gen to generate the ORM. However it doesn't create any of the relations (nested structs with foreign key, many2many etc hints).
I want these relations as I believe they are necessary to use the migrate feature to create the database correctly. I also want them so I can use the pre-loading feature to populate the structs in Go.
Example
- table
userhas a n to m relation to tableAccount - this relation is facilitated by a
User_Accounttable
CREATE TABLE user (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
UNIQUE (id)
)
CREATE TABLE account (
id INT NOT NULL AUTO_INCREMENT,
UNIQUE (id)
)
CREATE TABLE user_account (
user_id INT NOT NULL,
account_id INT NOT NULL,
PRIMARY KEY (user_id, account_id),
FOREIGN KEY(account_id) REFERENCES account (id)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(user_id) REFERENCES user (id)
)
I want Gorm Gen to look at this database and then generate structs like this:
type User struct {
ID uint `json:"id" gorm:"primary_key"`
Accounts []Account `json:"accounts" gorm:"many2many:user_account"`
}
type Account struct {
ID uint `json:"id" gorm:"primary_key"`
Name string `json:"name"`
}
The documentation here seems to imply that it's possible, but it doesn't make complete sense to me: https://gorm.io/gen/associations.html
If I use g.GenerateAllTable() with Gen generate g I get all of the table structs generated with all of the gorm and json hints that I want above, but it doesn't include any of the relation information. I.e it doesn’t create the field Accounts []Account above with the gorm hint many2many.
If instead I use:
accounts := g.GenerateModel("account")
users := g.GenerateModel("user",
gen.FieldRelate(field.Many2Many, "Account",
accounts,
&field.RelateConfig{
// RelateSlice: true,
GORMTag: field.GormTag{"foreignKey":[]string{"AccountID"},
"references": []string{"ID"}},
}),
)
g.ApplyBasic(accounts, users)
I get some pretty massive classes created with a lot of boilerplate code added for various SQL operations. It includes some relation information but it looks nothing like the structs above and doesn't include any gorm hints.
If I need to provide code as a above to explicitly define relations I can live with it, although I have about fifteen tables to deal with. However, I'm trying to match what I can achieve with python, which generates the entire ORM automatically by pointing it at an existing database and getting full migration and pre-loading support included.
I want to generate structs like thisif you want the structs to be generated then what is your input? And you said that you use gorm/geng.GenerateAllTable()to create the tables but the relations are missing. Does this mean the relations inside the database between tables are missing? What do you expect should GenerateAllTable do? Have you seen that generator_test.go has some meta data for the structs?