go-reform / reform

A better ORM for Go, based on non-empty interfaces and code generation.
https://gopkg.in/reform.v1
MIT License
1.44k stars 73 forks source link

Primary key assigned to a wrong column #237

Closed remy-sl closed 4 years ago

remy-sl commented 4 years ago

Describe the bug reform-db init assigns primary key to a wrong column

To Reproduce

  1. Given database schema
    CREATE TABLE `builds` (
      `i` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `id` varchar(32) NOT NULL,
      `name` text NOT NULL,
      PRIMARY KEY (`i`),
      UNIQUE KEY `id` (`id`)
    );
  2. Run reform-db -db-driver MySQL -db-source ... init /package/path
  3. Resulting model:
    type Builds struct {
        I    int64  `reform:"i"`
        ID   string `reform:"id,pk"`
        Name string `reform:"name"`
    }

Expected behavior Expected model is:

type Builds struct {
    I    int64  `reform:"i,pk"`
    ID   string `reform:"id"`
    Name string `reform:"name"`
}

Additional context I don't know what happened here. I am new to go so I actually don't want to dig deeper in the source code but I assume primary key recognition relies on a column name being id rather than actual PRIMARY KEY constraint.

AlekSi commented 4 years ago

No, it should rely on PRIMARY KEY. Sounds like a bug.

AlekSi commented 4 years ago

It will be fixed tomorrow in 1.4.1