- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.1k
Closed
Labels
Description
Describe the bug
If a field in a model is set to id: true using the @property decorator, then under normal behaviour, during service startup / migration, the respective MySQL database column should be set as primary key.
The bug consists of this setting of the primary key not happening.
For example, if the foo.databaseModel.ts file contains the following:
import {Entity, model, property} from '@loopback/repository';
@model({
  settings: {
    mysql: {table: 'foos'},
    forceId: true,
  }
})
export class FooDbEntry extends Entity {
  @property({
    type: 'string',
    id: true,
    generated: false,
    mysql: {
      columnName: 'foo_id',
      nullable: 'N'
    }
  })
  fooId: string;
  @property({
    generated: false,
    mysql: {
      columnName: 'bar',
      nullable: 'Y'
    }
  })
  bar: string | null;
}
then column foo_id is expected to be set as a private key. The bug consists if it remaining unset.
It's important to note, that if foo_id is already set as a primary key, it remains one after the startup / migration. This means that the startup / migration do not unset the primary key property of the column in the database.
Expected MySQL query output
mysql> SHOW KEYS FROM foos WHERE Key_name = 'PRIMARY';
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table  | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| foos |          0 | PRIMARY  |            1 | foo_id    | A         |         211 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
1 row in set (0.01 sec)
Actual MySQL query output
mysql> SHOW KEYS FROM foos WHERE Key_name = 'PRIMARY';
Empty set (0.00 sec)
Reproduction
Forked repo reproduction not practical due to need to spin up a database.