Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sequelize.js sequelize:除了一个表之外,没有生成索引_Sequelize.js - Fatal编程技术网

Sequelize.js sequelize:除了一个表之外,没有生成索引

Sequelize.js sequelize:除了一个表之外,没有生成索引,sequelize.js,Sequelize.js,在我的nodejs应用程序中,以下是sequelizejs 4.42.0生成db的方式: 数据库中有5个表,生成的表很好,但没有索引。只有一个表事件生成了索引,而其他4个表根本没有索引。以下是cmd输出: Executing (default): DROP TABLE IF EXISTS "events" CASCADE; Executing (default): DROP TABLE IF EXISTS "socketlists" CASCADE; Executing (default): D

在我的nodejs应用程序中,以下是sequelizejs 4.42.0生成db的方式:

数据库中有5个表,生成的表很好,但没有索引。只有一个表事件生成了索引,而其他4个表根本没有索引。以下是cmd输出:

Executing (default): DROP TABLE IF EXISTS "events" CASCADE;
Executing (default): DROP TABLE IF EXISTS "socketlists" CASCADE;
Executing (default): DROP TABLE IF EXISTS "veriflogs" CASCADE;
Executing (default): DROP TABLE IF EXISTS "users" CASCADE;
Executing (default): DROP TABLE IF EXISTS "users" CASCADE;
Executing (default): CREATE TABLE IF NOT EXISTS "users" ("id" INTEGER , "name" VARCHAR(255) NOT NULL, "email" VARCHAR(255), "cell" VARCHAR(255) NOT NULL, "cell_country_code" VARCHAR(255) NOT NULL, "comp_name" VARCHAR(255), "status" VARCHAR(255) NOT NULL, "role" VARCHAR(255) NOT NULL, "device_id" VARCHAR(255), "last_updated_by_id" INTEGER, "fort_token" VARCHAR(255) NOT NULL, "createdAt" TIMESTAMP WITH TIME ZONE, "updatedAt" TIMESTAMP WITH TIME ZONE, PRIMARY KEY ("id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'users' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
Executing (default): DROP TABLE IF EXISTS "veriflogs" CASCADE;
Executing (default): CREATE TABLE IF NOT EXISTS "veriflogs" ("id" INTEGER , "cell" VARCHAR(255), "cell_country_code" VARCHAR(255), "vcode" VARCHAR(255), "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'veriflogs' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
Executing (default): DROP TABLE IF EXISTS "socketlists" CASCADE;
Executing (default): CREATE TABLE IF NOT EXISTS "socketlists" ("id" INTEGER , "user_id" INTEGER, "socket_id" VARCHAR(255), "event_id" INTEGER, "server_id" VARCHAR(255), "active" BOOLEAN DEFAULT true, PRIMARY KEY ("id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'socketlists' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
Executing (default): DROP TABLE IF EXISTS "events" CASCADE;
Executing (default): CREATE TABLE IF NOT EXISTS "events" ("id" INTEGER , "name" VARCHAR(255) NOT NULL, "created_by_id" INTEGER NOT NULL, "event_info" JSONB, "current_step" VARCHAR(255), "access_list" INTEGER[], "event_step" VARCHAR(255)[], "event_snapshot" JSON, "event_category_id" INTEGER, "status" VARCHAR(255), "last_updated_by_id" INTEGER, "fort_token" VARCHAR(255) NOT NULL, "createdAt" TIMESTAMP WITH TIME ZONE, "updatedAt" TIMESTAMP WITH TIME ZONE, PRIMARY KEY ("id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'events' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
Executing (default): CREATE INDEX "events_name" ON "events" ("name")
Executing (default): CREATE INDEX "events_status" ON "events" ("status")
Executing (default): CREATE INDEX "events_fort_token" ON "events" ("fort_token")
Executing (default): CREATE INDEX "events_event_category_id" ON "events" ("event_category_id")
Executing (default): CREATE INDEX "events_created_by_id" ON "events" ("created_by_id")
Executing (default): DROP TABLE IF EXISTS "messages" CASCADE;
Executing (default): CREATE TABLE IF NOT EXISTS "messages" ("id" INTEGER , "event_id" INTEGER, "sender_id" INTEGER, "data" JSONB NOT NULL, "fort_token" VARCHAR(255) NOT NULL, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'messages' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
是什么导致其他4个表的索引丢失?以下是事件和用户的定义:


将indexes属性放在options对象中,与在事件模型中所做的相同

define只有三个参数,但在用户模型中添加了第四个参数,如果不处理额外的参数,则会在JavaScript中被忽略。只需将索引移动到与validate相同的对象中,即执行以下操作:

const User = db.define('user', {
   ...
}, {
   validate: {...}
   indexes: [...]
});

另外,我建议您对代码进行更好的格式化,以便这样的错误更容易被注意到:
const Event = db.define('event', {
    id: {type: Sql.INTEGER,
         primaryKey:true,
         min: 1},
    name: {type: Sql.STRING,
           min:2,
           allowNull: false,
        }, 
    created_by_id: {type: Sql.INTEGER,
              allowNull: false
            },
    event_info: {type: Sql.JSONB},
    current_step: {type: Sql.STRING},
    access_list: {type: Sql.ARRAY(Sql.INTEGER),
            },
    event_step: {type: Sql.ARRAY(Sql.STRING)},
    event_snapshot: {type: Sql.JSON},
    event_category_id: {type: Sql.INTEGER,
            },
    status: {type: Sql.STRING,
             isIn: ['active', 'cancelled', 'aborted', 'suspended', 'completed']},
    last_updated_by_id: {type: Sql.INTEGER},
    fort_token: {type: Sql.STRING,
                 allowNull: false,
                },
    createdAt: Sql.DATE,
    updatedAt: Sql.DATE
}, {
    indexes: [ {
        fields: ['name']
    },
    {
        fields: ['status']
    },
    {
        fields: ['fort_token']
    },{
        fields: ['event_category_id']
    }, {
        fields: ['created_by_id']
    } 
    ]
}); 

const User = db.define('user', {
    id: {type: Sql.INTEGER,
         primaryKey:true,
         min: 1},
    name: {type: Sql.STRING,
           allowNull: false,
           min: 2,
           max: 50,
    },
    email: {type: Sql.STRING,
            isEmail: true
    },      
    cell: {type: Sql.STRING,
            allowNull: false,
            min: 10,
            max: 20,
    },
    cell_country_code: {type: Sql.STRING,
                        allowNull: false,
    },
    comp_name: {type: Sql.STRING
    },
    status: {type: Sql.STRING,
             allowNull: false,
             isIn: ['active', 'blocked', 'inactive', 'pending']
    },
    role: {type: Sql.STRING,
           allowNull: false
    },
    device_id: {type: Sql.STRING,   //maybe empty when the user is initially created.
    },
    last_updated_by_id: {type: Sql.INTEGER},
    fort_token: {type: Sql.STRING,
                 allowNull: false,
                 min: 20  //64 for production
    },
    createdAt: Sql.DATE,
    updatedAt: Sql.DATE
  }, {
    validate: {async custom_validate() {
      let user = await User.findOne({where:{name :this.name, fort_token: this.fort_token}});
      if (user) throw new Error("用户重名!"); 
      let cell = await User.findOne({where: {cell: this.cell, cell_country_code: this.cell_country_code, status: 'active'}});
      if (cell) throw new Error("手机号已经注册使用!");
    }}
  }, { 
    indexes: [
      { 
        //For same fort_token, name to be unique
        unique: true,
        fields: ['name', 'fort_token']
      }, {
        //unique cell
        unique: true,
        fields: ['cell_country_code', 'cell', 'status']
      }, {
        fields: ['cell_country_code', 'cell']
      }, {
        //email
        fields: ['email']
      }, {
        fields: ['device_id']
      }, {
        fields: ['status']
      }, {
        fields: ['fort_token']
      }
    ]   

  });
const User = db.define('user', {
   ...
}, {
   validate: {...}
   indexes: [...]
});