Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stefanblaginov opened this issue Jan 25, 2022 · 0 comments · May be fixed by #460
Open

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

stefanblaginov opened this issue Jan 25, 2022 · 0 comments · May be fixed by #460
Labels

Comments

@stefanblaginov
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Icebox
Development

Successfully merging a pull request may close this issue.

1 participant