loopbackio / loopbackio/loopback-connector-mysql

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

Open
#461 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
126
Forks
181
Avg merge
4d 7h
Merged PRs (30d)
12

Description

## 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:
```ts
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;
}
```
4. Have the same field not set as a primary key inside the database.
5. 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the schema update with the shown FooDbEntry model, MySQL, and autoupdate enabled, then trace the connector's autoupdate handling for model properties marked id: true. Done means an existing table gains foo_id as its PRIMARY key without dropping its data.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mysql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.