Sequelize CLI 遷移錯誤:無法存取未定義的屬性(特別是「詳細資訊」)
P粉463418483
P粉463418483 2023-11-04 23:07:03
0
2
732

我正在運行 npx Sequelize-cli db:migrate 在資料庫中建立表格並返回以下訊息:

Sequelize CLI [Node: 18.9.1, CLI: 6.5.1, ORM: 6.20.1]


ERROR: Cannot find "/src/db/migrations/config/config.json". Have you run "sequelize init"?

ERROR: Cannot read properties of undefined (reading 'detail')
sequelize-cli db:migrate

Run pending migrations

但是我已經執行了命令sequelize-init...

這是我的 config.js 檔案

import dotenv from "dotenv";
dotenv.config();

export default {
  development: {
    username: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    dialect: process.env.DB_DIALECT,
    logging: true,
  },
  test: {
    username: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    host: process.env.DB_HOST,
    dialect: process.env.DB_DIALECT,
  },
  production: {
    username: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    host: process.env.DB_HOST,
    dialect: process.env.DB_DIALECT,
  },
};

我的遷移檔案:

"use strict";

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable("Users", {
      id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
      },
      name: {
        type: Sequelize.STRING(50),
        allowNull: false,
      },
      birth_date: {
        type: Sequelize.DATEONLY,
        allowNull: false,
      },
      email: {
        type: Sequelize.STRING(62),
        allowNull: false,
      },
      phone: {
        type: Sequelize.STRING(20),
        allowNull: false,
      },
      address: {
        type: Sequelize.STRING(100),
        allowNull: false,
      },
      password: {
        type: Sequelize.STRING(100),
        allowNull: false,
      },
      position_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: "Position",
          key: "id",
        },
      },
      is_active: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
      },
      is_admin: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    });
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable("Users");
  },
};

我的 .sequelizerc 檔案:

import path from "path";

export default {
  config: path.resolve("config", "config.js"),
};

和我的 package.json 檔案:

{
  "name": "startbootstrap-sb-admin-2-gh-pages",
  "version": "1.0.0",
  "description": "",
  "main": "gulpfile.js",
  "scripts": {
    "test": "test",
    "dev": "nodemon src/server.js"
  },
  "type": "module",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "connect-flash": "^0.1.1",
    "cookie-parser": "^1.4.6",
    "dotenv": "^16.0.3",
    "ejs": "^3.1.8",
    "express": "^4.18.1",
    "express-ejs-layouts": "^2.5.1",
    "express-session": "^1.17.3",
    "http": "^0.0.1-security",
    "http-error": "^0.0.6",
    "morgan": "^1.10.0",
    "mysql2": "^2.3.3",
    "nodemon": "^2.0.16",
    "path": "^0.12.7",
    "sequelize": "^6.20.1"
  },
  "devDependencies": {
    "sequelize-cli": "^6.5.1"
  }
}

我想執行遷移,但無法成功,即使我將設定檔更改為 json,錯誤仍然存在...

P粉463418483
P粉463418483

全部回覆(2)
P粉551084295

在 models/index.js 檔案中,您需要將 config.json 更新為 config.js

#
P粉098417223

在 config/config.js 中,

  • 您是否嘗試使用 cont dotenv = require('dotenv'); 而不是 import dotenv from "dotenv";

  • module.exports = 取代 export default

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板