模型 table 未在 mysql 环回 3 中自动创建

Model table not getting auto created in mysql loopback 3

我是 Loopback 框架的新手。 我经历了入门和模型创建并尝试创建测试应用程序。 我通过命令创建了应用程序 -> lb
并通过 -> lb datasource
创建了 mysql 数据源 并通过 -> lb model
创建了两个模型 正在使用 mysql 数据源。

{
  "name": "Brand",
  "plural": "brands",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

所以现在当我通过 npm start 启动应用程序并转到 localhost:3000/explorer 并尝试使用任何终点时,我得到以下 table not found 错误。 Unhandled error for request GET /api/brands: Error: ER_NO_SUCH_TABLE: Table 'loop3.Brand' doesn't exist

Loopback 会自动在数据库中创建 tables 吗??或者我必须为每个模型创建 autoMigrate 文件或者应该如何完成请帮助..

答案:
感谢@Diana,我在 boot 目录中创建了 script.js 文件并添加了以下代码

'use strict';

module.exports = function(app) {
  var db = app.datasources.mysqld;

  db.autoupdate(function(err) {
    if (err) throw err;
    console.log('\nAutomigrate completed');
    // other code here
  });
};

它创建 table 并在任何 属性 发生变化时进行更新。

您需要 运行 automigrate 函数在数据库中创建 table 模式。您可以在启动脚本中添加它,每次启动应用程序时它都会得到 运行。以这个为例: https://github.com/ssh24/loopback-sandbox/blob/master/apps/mysql/mysql-101/server/boot/script.js

请注意,automigrate 函数将删除现有的 table 并重新创建它。这意味着任何现有数据都将丢失。 autoupdate 函数可以代替。有关详细信息,请参阅此文档页面:https://loopback.io/doc/en/lb3/Creating-a-database-schema-from-models.html