【发布时间】:2021-07-16 01:16:50
【问题描述】:
我将正在开发的 Web 应用程序从 Windows 克隆到 Ubuntu 机器中。这个应用程序具有模块化结构,包含一个 Node JS 服务器、一个 MYSQL DB 和一个带有 React 的客户端。
我正在为数据库控制器使用 Sequelize CLI。
当我在 Ubuntu 中创建数据库并运行迁移时,我得到了
错误:无法创建表
icsi_app_development.phases(错误号:150 "外键约束格式不正确")
这在具有相同依赖关系的 Windows 中运行良好:
Sequelize CLI [节点:14.17.2,CLI:6.2.0,ORM:6.6.2]
对此关联错误有任何想法吗?非常感谢!
这是我的 create-phase.js
'use strict';
module.exports = {
up: async (queryInterface, DataTypes) => {
await queryInterface.createTable('phases', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
uuid: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4
},
projectId: {
type: DataTypes.INTEGER,
references: {model: "Projects", key: "id"},
onDelete: "CASCADE"
},
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
description: {
type: DataTypes.STRING,
allowNull: false
},
isActive: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
},
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
}
});
},
down: async (queryInterface, DataTypes) => {
await queryInterface.dropTable('phases');
}
};
这是我的 create-project.js
'use strict';
module.exports = {
up: async (queryInterface, DataTypes) => {
await queryInterface.createTable('projects', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
uuid: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4
},
nume: {
type: DataTypes.STRING,
allowNull: false
},
cod_identificare: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
departament: {
type: DataTypes.STRING,
allowNull: false
},
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
}
});
},
down: async (queryInterface, DataTypes) => {
await queryInterface.dropTable('projects');
}
};
这是我的项目模型
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Project extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
// toJSON(){
// return {...this.get(), id: undefined}
// }
};
Project.init({
nume: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: {msg: "Proiectul trebuie sa aiba un nume"},
notEmpty: {msg: "Proiectul trebuie sa aiba un nume"}
}
},
cod_identificare: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
notNull: {msg: "Proiectul trebuie sa prezinte un cod de identificare unic"},
notEmpty: {msg: "Proiectul trebuie sa prezinte un cod de identificare unic"}
}
},
departament: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: {msg: "Proiectul trebuie sa fie atribuit unui departament"},
notEmpty: {msg: "Proiectul trebuie sa fie atribuit unui departament"}
}
},
uuid: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4
}
},
{
sequelize,
tableName: "projects",
modelName: 'Project',
});
Project.associate = (models) => {
Project.hasMany(models.Phase, {
foreignKey: 'projectId',
as: "phases"
});
Project.belongsToMany(models.User, {
foreignKey: 'projectId',
through: 'users_projects',
as: "users"
});
}
return Project;
};
这是我的阶段模型
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Phase extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
};
Phase.init({
projectId: {
type: DataTypes.INTEGER,
allowNull: false
},
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
notEmpty: {msg: "Faza proiectului trebuie sa prezinte un titlu"},
notNull: {msg: "Faza proiectului trebuie sa prezinte un titlu"}
}
},
description: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: {msg: "Faza proiectului trebuie sa prezinte o descriere"},
notNull: {msg: "Faza proiectului trebuie sa prezinte o descriere"}
}
},
isActive: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
},
uuid: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4
}
}, {
sequelize,
tableName: "phases",
modelName: 'Phase',
});
Phase.associate = (models) => {
Phase.belongsTo(models.Project, {
foreignKey: 'projectId',
as: "projects"
});
Phase.hasMany(models.File, {
foreignKey: "phaseId",
as: "files"
});
}
return Phase;
};
【问题讨论】:
-
共享完整运行项目,或共享所有模型
标签: javascript mysql node.js sequelize.js