使用 MySQL 方言的 sequelize 模型生成
Model Generation using sequelize with MySQL dialect
关于使用 sequelize 生成模型的查询:
- 如何使用 MySQL.
在 sequelize 中提供自增自定义值
- 如何将当前时间戳设置为任何字段作为数据类型。 (查询中显示的当前实现对我不起作用!)
- 如何设置 ROW_FORMAT = COMPACT?
- 如何在Unique Key或Foreign Key中自定义约束名称?
需要进行哪些更改?以及在哪个文件模型或迁移中。
我创建的内容:
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_INCREMENT 只是指它自己的记录计数,即,如果我们有 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',
}
);
关于使用 sequelize 生成模型的查询:
- 如何使用 MySQL. 在 sequelize 中提供自增自定义值
- 如何将当前时间戳设置为任何字段作为数据类型。 (查询中显示的当前实现对我不起作用!)
- 如何设置 ROW_FORMAT = COMPACT?
- 如何在Unique Key或Foreign Key中自定义约束名称?
需要进行哪些更改?以及在哪个文件模型或迁移中。
我创建的内容:
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_INCREMENT 只是指它自己的记录计数,即,如果我们有 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',
}
);