简体   繁体   中英

Model Generation using sequelize with MySQL dialect

Queries Regarding Model Generation using sequelize:

  1. How to provide auto-increment custom value in sequelize using MySQL.
  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?
  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:

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

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

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. Shown snippet for Foreign Key:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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