loopbackio / loopback-datasource-juggler

Connect Loopback to various Data Sources
http://www.loopback.io
Other
277 stars 364 forks source link

the function `discoverSchemas` no longer discover any relations #2339

Open thiscode opened 5 days ago

thiscode commented 5 days ago

The function discoverSchemas no longer discover any relations since the commit b4295fdf9786770bee89001685a1a568c1718504.

https://github.com/loopbackio/loopback-datasource-juggler/commit/b4295fdf9786770bee89001685a1a568c1718504

Before the commit, the array results had a maximum of 3 elements. Thus, the relations were correctly read from position [2]:

  const tasks = [
    this.discoverModelProperties.bind(this, tableName, options),
    this.discoverPrimaryKeys.bind(this, tableName, options);

  const followingRelations = options.associations || options.relations;
  if (followingRelations) {
    tasks.push(this.discoverForeignKeys.bind(this, tableName, options));
  }

  async.parallel(tasks, function(err, results) {
    //...
    const columns = results[0];
    //...
    const primaryKeys = results[1] || [];
    //...
    if (followingRelations) {
      //...
      const foreignKeys = results[2] || [];
      //...
    }
  }

However, after the commit, the unique keys are in this position. The index position was forgotten to be adjusted further down:

  const tasks = [
    this.discoverModelProperties.bind(this, tableName, options),
    this.discoverPrimaryKeys.bind(this, tableName, options),
    this.discoverUniqueKeys.bind(this, tableName, options)];  // <------------- This was added

  const followingRelations = options.associations || options.relations;
  if (followingRelations) {
    tasks.push(this.discoverForeignKeys.bind(this, tableName, options));
  }

  async.parallel(tasks, function(err, results) {
    //...
    const columns = results[0];
    const uniqueKeyArray = results[2] || [];
    //...
    const primaryKeys = results[1] || [];
    //...
    if (followingRelations) {
      //...
      const foreignKeys = results[2] || []; // <---------------- This is now incorrect
      //...
    }
  }

Steps to reproduce

The loopback cli tool discover will not create model with relations when used with the option --relations=true and the connector loopback-connector-mysql.

Current Behavior

The loopback cli tool discover will not create model with relations when used with the option --relations=true and the connector loopback-connector-mysql.

Expected Behavior

The loopback cli tool discover should create model with relations when used with the option --relations=true and the connector loopback-connector-mysql.

Link to reproduction sandbox

Additional information

Related Issues

See Reporting Issues for more tips on writing good issues

thiscode commented 5 days ago

Here is some more detail after I looked into it more closely: Loopback uses @loopback/repository ^7.0.8 as a dependency in the latest version. @loopback/repository in turn uses loopback-datasource-juggler ^5.1.2 as a dependency. However, loopback-datasource-juggler has been broken since ^5.1.0. A workaround is to downgrade the loopback application in the following order:

npm i @loopback/repository@7.0.7 --save-exact
npm i loopback-datasource-juggler@5.0.12 --save-exact

After that downgrade the discoverSchema works again.

thiscode commented 5 days ago

The cli tool lb4 discover --relations=true will show a warning:

The project was originally generated by @loopback/cli@6.1.2.
The following dependencies are incompatible with @loopback/cli@6.1.2:
dependencies
- @loopback/repository: 7.0.7 (cli ^7.0.8)

But choosing Skip upgrading project dependencies it will continue and the relations will be discovered correctly.

dhmlau commented 3 days ago

@aaqilniz, could you please take a look? Thanks.