loopback-connector-mysql icon indicating copy to clipboard operation
loopback-connector-mysql copied to clipboard

Model's ID not set as private key in MySQL database

Open stefanblaginov opened this issue 3 years ago • 0 comments

Steps to reproduce

  1. Have a LoopBack 4 microservice using a MySQL database.
  2. Have important data in the MySQL database belonging to the service (making automigrate-ing by drop-ing the database unfeasible).
  3. Have one of the model's fields set as a an id, by having id: true set in its @property decorator, like so:
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;
}
  1. Have the same field not set as a primary key inside the database.
  2. Restart the service with autoupdate set to be executed during startup.

Current Behavior

The field set as an id inside the model file, is not reflected as a primary key inside the database.

Actual MySQL query output

mysql> SHOW KEYS FROM foos WHERE Key_name = 'PRIMARY';
Empty set (0.00 sec)

Expected Behavior

The field set as an id inside the model file, is reflected as a primary key inside 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)

Link to reproduction sandbox

Not applicable due to requirement to connect to MySQL database.

stefanblaginov avatar Jan 25 '22 18:01 stefanblaginov