1

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 user has a n to m relation to table Account
  • this relation is facilitated by a User_Account table
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.

5
  • 1
    It is unclear to me what you are asking above. You wrote I want to generate structs like this if you want the structs to be generated then what is your input? And you said that you use gorm/gen g.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? Commented Aug 7, 2024 at 16:02
  • Thanks for commenting. My input is an existing database. From that, I want Gorm Gen to create the ORM, i.e the structs. This should include the field "Accounts []Account ... many2many:user_account", which Gorm Gen does not generate. I want it to generate this too, as it allows me to preload related accounts into a user object when retrieving data. In my tests I also want the migrate feature to create a new blank database with all of the tables to run tests against. For this I also need that Accounts field so that migrate generate the foreign key constraints. I updated the question too. Commented Aug 7, 2024 at 16:15
  • 1
    Possibly related Many2Many fails to create a join table record and How to make the many2many gorm documentation example work? Commented Aug 7, 2024 at 21:41
  • Thanks again, I checked those out and neither seem to offer a solution to my problem. I have a follow up problem where I skipped ahead of this problem by manually adding the relationship field, then ran into an auto migrate error: github.com/go-gorm/gorm/issues/7143 (also posted here under my profile) But frustratingly, my issue was marked stale because I don't provide a playground fork with a running example, because the playground repo fails too!! I'm just going to get Go to load my python ORM to generate everything for tests 🙄 Commented Aug 7, 2024 at 22:37
  • I am also running through the same issue with gorm gen where the relationship are jot being added to the generated model. Did you find any solution or and workarounds regarding this issue? Commented Nov 15, 2025 at 5:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.