gpt4 book ai didi

mysql - Sequelize - 创建 1 对 N 关系时标识符名称 'xxx_idx' 太长

转载 作者:行者123 更新时间:2023-12-03 22:20:31 25 4
gpt4 key购买 nike

我有 2 个表 physical-assessment-exercisepsysical-assessment-lesson
心理评估-lesson.model.js

const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const result = sequelizeClient.define('physical_assessment_lesson', {
...
});

// eslint-disable-next-line no-unused-vars
result.associate = function (models) {
result.hasMany(models.psychological_assessment_answer);
};

return result;
};

物理评估-exercise.model.js
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const exercise = sequelizeClient.define('physical_assessment_exercise', {
...
});

// eslint-disable-next-line no-unused-vars
exercise.associate = function (models) {
exercise.belongsTo(models.physical_assessment_lesson);
};

return exercise;
};

保存上述文件后,控制台显示以下错误:
{  "code":"ER_TOO_LONG_IDENT","errno":1059,"sqlState":"42000",
"sqlMessage":
"Identifier name
'physical_assessment_lesson_psychologicalAssessmentAnswerId_foreign_idx' is too long"
}
我应该如何解决这个问题并自定义标识符名称?

最佳答案

Mysql 只允许 64 个字符来命名它的 inde,你可以在这里看到:https://dev.mysql.com/doc/refman/8.0/en/identifier-length.html
您的索引名称当前包含 70 个字符,因此您有多种解决方案选项:

  • 你可以重命名你的实体,这样名字就不会那么长(例如,“评估”可以是“屁股”,“ body ”可以是“物理”,“心理”可以是“心理”——不要忘记记住,什么是这些术语的简短变体意味着:-)
  • 定义 key 而不是使用默认名称,如下所示:https://sequelize.org/master/manual/indexes.html 并指定一个名称,该名称少于 64 个字符。
  • 关于mysql - Sequelize - 创建 1 对 N 关系时标识符名称 'xxx_idx' 太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67830952/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com