为什么即使我调整了模型中的数据类型,Sequelize 中仍不断收到“ER_DATA_TOO_LONG”错误消息?

问题描述 投票:0回答:1

我正在使用 Sequelize 和 MySQL 在我的架构中创建一个表。我已经为我的桌子创建了模型,但我在其中一个模型上遇到了麻烦。这是它的代码片段:

const Author = require('./author');
const Genre = require('./genre'); 

const Book = sequelize.define('Book', {
  title: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  summary: {
    type: DataTypes.TEXT,
    allowNull: false,
  },
  isbn: {
    type: DataTypes.STRING,
    allowNull: false,
  },
});

Book.belongsTo(Author, { as: 'author' });
Book.hasMany(Genre, { as: 'genres' });

我还使用 Node.js 文件来实际创建表并向其中插入数据。以下是文件中的一个函数,它基于 Book 模型创建一本书并将其插入到 book 表中:

async function bookCreate(index, title, summary, isbn, author, genre) {
  const bookdetail = {
    title: title,
    summary: summary,
    author: author,
    isbn: isbn,
  };
  if (genre != false) bookdetail.genre = genre;

  const book = await Book.create(bookdetail);
  await book.save();
  books[index] = book;
  console.log(`Added book: ${title}`);
}

// Function being called
bookCreate(0,
      "The Name of the Wind (The Kingkiller Chronicle, #1)",
      "I have stolen princesses back from sleeping barrow kings. I burned down the town of Trebon. I have spent the night with Felurian and left with both my sanity and my life. I was expelled from the University at a younger age than most people are allowed in. I tread paths by moonlight that others fear to speak of during day. I have talked to Gods, loved women, and written songs that make the minstrels weep.",
      "9781473211896",
      authors[0],
      [genres[0]]
    )

当我尝试使用

node
命令运行 Node.js 文件时,我在终端中收到此错误,提示摘要数据太长:

code: 'ER_DATA_TOO_LONG',
    errno: 1406,
    sqlState: '22001',
    sqlMessage: "Data too long for column 'summary' at row 1",
    sql: 'INSERT INTO `Books` (`id`,`title`,`summary`,`isbn`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?);',
    parameters: [
      'The Name of the Wind (The Kingkiller Chronicle, #1)',
      'I have stolen princesses back from sleeping barrow kings. I burned down the town of Trebon. I have spent the night with Felurian and left with both my sanity and my life. I was expelled from the University at a younger age than most people are allowed in. I tread paths by moonlight that others fear to speak of during day. I have talked to Gods, loved women, and written songs that make the minstrels weep.',
      '9781473211896',
      '2023-11-28 16:14:10',
      '2023-11-28 16:14:10'
    ]

我不确定为什么会收到该错误,因为我将 Book 模型的

summary
属性的数据类型设置为
TEXT
而不是
STRING
,我相信这应该禁用长度检查,但是我可能是错的。有人可以帮助我吗?

另外,为了澄清起见,这里是 Node.js 文件夹中的函数,用于连接到数据库、同步模型、执行必要的功能,然后断开连接:

main().catch((err) => console.log(err));

async function main() {
  console.log("Debug: About to connect");
  await sequelize.authenticate()
  console.log("Debug: Should be connected?");
  await Author.sync()
  await Book.sync()
  await BookInstance.sync()
  await Genre.sync()

  await createGenres();
  await createAuthors();
  await createBooks();
  await createBookInstances();
  console.log("Debug: Closing Sequelize");
  sequelize.close();
}
javascript mysql node.js sequelize.js
1个回答
-1
投票

看起来代码开始插入到列

id
中,但随后没有为
id
提供值。

parameters
中的列数不应该与
INSERT
中的列名称数量匹配吗?

© www.soinside.com 2019 - 2024. All rights reserved.