How do you generate Gorm tables with relations?

First some background for what I’m trying to achieve, in-case there’s a better approach, rendering my question a moot point.

I have a Go project and I’m adding functional tests. I want the tests to 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). I want the test to then create all of the tables in the database, add some test data, run the tests and then delete everything.

In Python I used SqlAlchemy and Alembic to create a database from scratch with SQL then have the code look at that database and create an ORM that reflects it completely, including indexes and relations. I could use the migrate features on this generated ORM to bring up the database in my test environment as outlined above.

In Go I’m already invested in Gorm, so 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. 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.

For example, I want to 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 tables generated with all of the gorm and json hints that I want above, but it doesn’t include any of the relation information. 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 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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật