使用原始字符串进行Knex迁移

时间:2016-09-08 19:18:05

标签: mysql knex.js

我尝试使用原始sql运行knex迁移来创建表,而不是在knex中写出来。这里出了什么问题?我使用es6模板字符串来跨越多行,但它没有迁移。

这是我遇到的错误

  

错误:ER_PARSE_ERROR:您的SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以便在#CREIDE TABLE mg_customer_entity_varchar附近使用正确的语法(         value_id int(11)NOT NULL AU'在第22行

这是我的代码:

require('babel-register')


exports.up = function(knex, Promise) {
  let raw = `
    CREATE TABLE \`mg_customer_entity\` (
      \`entity_id\` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Entity Id',
      \`entity_type_id\` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Entity Type Id',
      \`attribute_set_id\` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Attribute Set Id',
      \`website_id\` smallint(5) unsigned DEFAULT NULL COMMENT 'Website Id',
      \`email\` varchar(255) DEFAULT NULL COMMENT 'Email',
      \`group_id\` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Group Id',
      \`increment_id\` varchar(50) DEFAULT NULL COMMENT 'Increment Id',
      \`store_id\` smallint(5) unsigned DEFAULT '0' COMMENT 'Store Id',
      \`created_at\` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Created At',
      \`updated_at\` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Updated At',
      \`is_active\` smallint(5) unsigned NOT NULL DEFAULT '1' COMMENT 'Is Active',
      \`disable_auto_group_change\` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Disable automatic group change based on VAT ID',
      PRIMARY KEY (\`entity_id\`),
      UNIQUE KEY \`UNQ_MG_CUSTOMER_ENTITY_EMAIL_WEBSITE_ID\` (\`email\`,\`website_id\`),
      KEY \`IDX_mg_CUSTOMER_ENTITY_STORE_ID\` (\`store_id\`),
      KEY \`IDX_mg_CUSTOMER_ENTITY_ENTITY_TYPE_ID\` (\`entity_type_id\`),
      KEY \`IDX_mg_CUSTOMER_ENTITY_EMAIL_WEBSITE_ID\` (\`email\`,\`website_id\`),
      KEY \`IDX_mg_CUSTOMER_ENTITY_WEBSITE_ID\` (\`website_id\`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Customer Entity';

    CREATE TABLE \`mg_customer_entity_varchar\` (
      \`value_id\` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Value Id',
      \`entity_type_id\` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Entity Type Id',
      \`attribute_id\` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Attribute Id',
      \`entity_id\` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Entity Id',
      \`value\` varchar(255) DEFAULT NULL COMMENT 'Value',
      PRIMARY KEY (\`value_id\`),
      UNIQUE KEY \`UNQ_mg_CUSTOMER_ENTITY_VARCHAR_ENTITY_ID_ATTRIBUTE_ID\` (\`entity_id\`,\`attribute_id\`),
      KEY \`IDX_mg_CUSTOMER_ENTITY_VARCHAR_ENTITY_TYPE_ID\` (\`entity_type_id\`),
      KEY \`IDX_mg_CUSTOMER_ENTITY_VARCHAR_ATTRIBUTE_ID\` (\`attribute_id\`),
      KEY \`IDX_mg_CUSTOMER_ENTITY_VARCHAR_ENTITY_ID\` (\`entity_id\`),
      KEY \`IDX_mg_CUSTOMER_ENTITY_VARCHAR_ENTITY_ID_ATTRIBUTE_ID_VALUE\` (\`entity_id\`,\`attribute_id\`,\`value\`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Customer Entity Varchar';
  `
  return knex.schema.raw(raw)
};

exports.down = function(knex, Promise) {
  return knex.schema
    .dropTableIfExists('mg_customer_entity')
    .dropTableIfExists('mg_customer_entity_varchar')
};

1 个答案:

答案 0 :(得分:2)

  1. 我没有正确使用babel-register
  2. 必须将updated_at默认值从'0000-00-00 00:00:00'更改为CURRENT_TIMESTAMP
  3.   

    迁移/ 20160908144316_beerhawkAuth.js

    require('babel-register')
    let value = require('../migrations-es6/20160908144316_beerhawkAuth.js')
    exports.up = value.up
    exports.down = value.down
    
      

    迁移-ES6 / 20160908144316_beerhawkAuth.js

    export let up = function(knex, Promise) {
      let create_mg_customer_entity = `
      CREATE TABLE \`mg_customer_entity\` (
        \`entity_id\` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Entity Id',
        \`entity_type_id\` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Entity Type Id',
        \`attribute_set_id\` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Attribute Set Id',
        \`website_id\` smallint(5) unsigned DEFAULT NULL COMMENT 'Website Id',
        \`email\` varchar(255) DEFAULT NULL COMMENT 'Email',
        \`group_id\` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Group Id',
        \`increment_id\` varchar(50) DEFAULT NULL COMMENT 'Increment Id',
        \`store_id\` smallint(5) unsigned DEFAULT '0' COMMENT 'Store Id',
        \`created_at\` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Created At',
        \`updated_at\` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Updated At',
        \`is_active\` smallint(5) unsigned NOT NULL DEFAULT '1' COMMENT 'Is Active',
        \`disable_auto_group_change\` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Disable automatic group change based on VAT ID',
        PRIMARY KEY (\`entity_id\`),
        UNIQUE KEY \`UNQ_MG_CUSTOMER_ENTITY_EMAIL_WEBSITE_ID\` (\`email\`,\`website_id\`),
        KEY \`IDX_mg_CUSTOMER_ENTITY_STORE_ID\` (\`store_id\`),
        KEY \`IDX_mg_CUSTOMER_ENTITY_ENTITY_TYPE_ID\` (\`entity_type_id\`),
        KEY \`IDX_mg_CUSTOMER_ENTITY_EMAIL_WEBSITE_ID\` (\`email\`,\`website_id\`),
        KEY \`IDX_mg_CUSTOMER_ENTITY_WEBSITE_ID\` (\`website_id\`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Customer Entity';
      `
      let create_mg_customer_entity_varchar = `
      CREATE TABLE \`mg_customer_entity_varchar\` (
        \`value_id\` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Value Id',
        \`entity_type_id\` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Entity Type Id',
        \`attribute_id\` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Attribute Id',
        \`entity_id\` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Entity Id',
        \`value\` varchar(255) DEFAULT NULL COMMENT 'Value',
        PRIMARY KEY (\`value_id\`),
        UNIQUE KEY \`UNQ_mg_CUSTOMER_ENTITY_VARCHAR_ENTITY_ID_ATTRIBUTE_ID\` (\`entity_id\`,\`attribute_id\`),
        KEY \`IDX_mg_CUSTOMER_ENTITY_VARCHAR_ENTITY_TYPE_ID\` (\`entity_type_id\`),
        KEY \`IDX_mg_CUSTOMER_ENTITY_VARCHAR_ATTRIBUTE_ID\` (\`attribute_id\`),
        KEY \`IDX_mg_CUSTOMER_ENTITY_VARCHAR_ENTITY_ID\` (\`entity_id\`),
        KEY \`IDX_mg_CUSTOMER_ENTITY_VARCHAR_ENTITY_ID_ATTRIBUTE_ID_VALUE\` (\`entity_id\`,\`attribute_id\`,\`value\`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Customer Entity Varchar';
      `
      return knex.schema
      .raw(create_mg_customer_entity)
      .raw(create_mg_customer_entity_varchar)
    };
    
    export let down = function(knex, Promise) {
      return knex.schema
        .dropTableIfExists('mg_customer_entity')
        .dropTableIfExists('mg_customer_entity_varchar')
    };