关于使用sequelize生成模型的查询:
- 如何使用MySQL在sequelize中提供自动递增自定义值。
- 如何将当前时间戳设置为任何字段作为数据类型。 (查询中显示的当前实现对我不起作用!)
- 如何设置 ROW_FORMAT = COMPACT?
- 如何自定义唯一键或外键中的约束名称?
需要进行哪些更改?以及模型或迁移在哪个文件中。
我创建的内容:
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/