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 #8252

Closed
stefanblaginov opened this issue Jan 20, 2022 · 1 comment
Closed

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

stefanblaginov opened this issue Jan 20, 2022 · 1 comment
Labels
bug db:MySQL Topics specific to MySQL

Comments

@stefanblaginov
Copy link

stefanblaginov commented Jan 20, 2022

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.

@achrinza achrinza added the db:MySQL Topics specific to MySQL label Jan 22, 2022
@stefanblaginov
Copy link
Author

Recreated inside loopbackio/loopback-connector-mysql repo: loopbackio/loopback-connector-mysql#461

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug db:MySQL Topics specific to MySQL
Projects
None yet
Development

No branches or pull requests

2 participants