mysql - 使用 Sequelize 和 MySQL 方言生成模型

标签 mysql database-design sequelize.js constraints auto-increment

关于使用sequelize生成模型的查询:

  1. 如何使用MySQL在sequelize中提供自动递增自定义值。
  2. 如何将当前时间戳设置为任何字段作为数据类型。 (查询中显示的当前实现对我不起作用!)
  3. 如何设置 ROW_FORMAT = COMPACT?
  4. 如何自定义唯一键或外键中的约束名称?

需要进行哪些更改?以及模型或迁移在哪个文件中。

我创建的内容:

CREATE TABLE `tbltests` (
  `testId` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(250) DEFAULT NULL,
  `nameId` int(4) unsigned DEFAULT 0,
  `nameunique` varchar(100) NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT 0,
  `timet` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `createdAt` datetime NOT NULL,
  `updatedAt` datetime NOT NULL,
  PRIMARY KEY (`testId`),
  UNIQUE KEY `nameunique` (`nameunique`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

我想要的输出:

 CREATE TABLE `tbltests` (
      `testId` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(250) DEFAULT NULL,
      `nameId` int(4) unsigned DEFAULT 0,
      `nameunique` varchar(100) NOT NULL,
      `status` tinyint(1) NOT NULL DEFAULT 0,
      `timet` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
      `createdAt` datetime NOT NULL,
      `updatedAt` datetime NOT NULL,
      PRIMARY KEY (`testId`),
      UNIQUE KEY `nameunique` (`nameunique`)
    ) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT

最佳答案

关于使用sequelize生成模型的解决方案:

How to provide auto-increment custom value in sequelize using MySQL?

如所问问题中的 CreateTable 查询所示,AUTO_INCRMENT 仅指其自己的记录计数,即,如果我们有 2 条记录,则查询语句将显示计数为 AUTO_INCREMENT = 2

How to customize constraint name in Unique Key or Foreign Key?

使用addConstraint()函数,我们可以添加外键和唯一键的约束以及自定义约束名称。显示外键片段:

return queryInterface.addConstraint(
'Tabletests',
['GeneratedBy'],
  {
    type: 'foreign key',
    name: 'GeneratedBy_FK_1',
    references: {
      table: 'TableUsers',
      field: 'Uid'
    },
    onUpdate: 'cascade',
  }
);

关于mysql - 使用 Sequelize 和 MySQL 方言生成模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59692714/

相关文章:

javascript - 如何使用 PHP、javascript mysql 事务将输入的数组值插入到数据库中

php - 在 MySQL (PHP) 中使用 ft_min_word_len 下的字母进行全文搜索

mysql - MySQL中最小日期的提取量

mysql 表格中的矩阵 View

database - 我必须考虑什么样的不同关系?

mysql - n 到多个数据库关系

node.js - Sequelize 和 PostGIS 几何列未返回正确的 JSON

node.js - 将 Passport 使用从 Mongoose 转换为 Sequelize

php - 如何为访问数据库的应用程序分段数据库(也称为多个用户问题的单个数据库)?

node.js - 获取 Sequelize BelongsToMany 关联的计数