简体   繁体   English

使用 sequelize 和 MySQL 方言生成模型

[英]Model Generation using sequelize with MySQL dialect

Queries Regarding Model Generation using sequelize:关于使用 sequelize 生成模型的查询:

  1. How to provide auto-increment custom value in sequelize using MySQL.如何使用 MySQL 在 sequelize 中提供自动递增的自定义值。
  2. How can I set the current timestamp to any field as a datatype.如何将当前时间戳设置为任何字段作为数据类型。 (Current implementation shown in query didn't work for me!) (查询中显示的当前实现对我不起作用!)
  3. How to set ROW_FORMAT = COMPACT?如何设置 ROW_FORMAT = COMPACT?
  4. How to customize constraint name in Unique Key or Foreign Key?如何在唯一键或外键中自定义约束名称?

Which changes required?需要哪些改变? and in which file Model OR Migration.和在哪个文件模型或迁移。

What I have created:我创造了什么:

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

What I want as output:我想要的输出:

 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

Solutions Regarding Model Generation using sequelize:关于使用 sequelize 生成模型的解决方案:

How to provide auto-increment custom value in sequelize using MySQL?如何使用 MySQL 在 sequelize 中提供自动递增的自定义值?

As shown in CreateTable query in asked question, AUTO_INCREMENT simply refers to its own records count ie, if we have 2 records the query statement will show that count as AUTO_INCREMENT = 2如所问问题中的CreateTable查询所示,AUTO_INCREMENT 只是指它自己的记录计数,即,如果我们有 2 条记录,查询语句将显示计数为AUTO_INCREMENT = 2

How to customize constraint name in Unique Key or Foreign Key?如何在唯一键或外键中自定义约束名称?

Using addConstraint() function we can add constraints for Foreign Key and unique key as well with custom constraint name too.使用addConstraint()函数,我们也可以为外键和唯一键添加约束以及自定义约束名称。 Shown snippet for Foreign Key:显示的外键片段:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM