Skip to main content
edited tags
Link
surfmuggle
  • 6.1k
  • 8
  • 60
  • 98
code hints added, scrollbar removed
Source Link
surfmuggle
  • 6.1k
  • 8
  • 60
  • 98
  • 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 of the tables in the database, 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 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).

For example, In my database I have a User table with a many to many connection with an Account table. This many to many connection is facilitated in the database by a User_Account table with a user ID and account ID. The SQL to generate this for a MySQL database is this:Example

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)
)
  • 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)
)
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"`
}
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"`
}

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 Accounts field Accounts []Account above with the many2many 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)
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)
  • 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 of the tables in the database, 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).

For example, In my database I have a User table with a many to many connection with an Account table. This many to many connection is facilitated in the database by a User_Account table with a user ID and account ID. The SQL to generate this for a MySQL database is this:

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)
)
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"`
}

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 Accounts field above with the many2many gorm hint. 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)
  • 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).

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)
)
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"`
}

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)
Added SQL commands to create the database in question
Source Link
LaserJesus
  • 8.6k
  • 9
  • 53
  • 69

For example, In my database I have a User table with a many to many connection with an Account table. This many to many connection is facilitated in the database by a User_Account table with a user ID and account ID. The SQL to generate this for a MySQL database is this:

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:

For example, In my database I have a User table with a many to many connection with an Account table. This many to many connection is facilitated in the database by a User_Account table with a user ID and account ID. I want Gorm Gen to look at this database and then generate structs like this:

For example, In my database I have a User table with a many to many connection with an Account table. This many to many connection is facilitated in the database by a User_Account table with a user ID and account ID. The SQL to generate this for a MySQL database is this:

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:

Fixed typo
Source Link
LaserJesus
  • 8.6k
  • 9
  • 53
  • 69
Loading
added more information based on feedback
Source Link
LaserJesus
  • 8.6k
  • 9
  • 53
  • 69
Loading
added more information based on feedback
Source Link
LaserJesus
  • 8.6k
  • 9
  • 53
  • 69
Loading
shortened
Source Link
surfmuggle
  • 6.1k
  • 8
  • 60
  • 98
Loading
Source Link
LaserJesus
  • 8.6k
  • 9
  • 53
  • 69
Loading