Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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
Node.js 赛欧postgresql多对多协会不起作用_Node.js_Postgresql_Many To Many_Sails.js_Waterline - Fatal编程技术网

Node.js 赛欧postgresql多对多协会不起作用

Node.js 赛欧postgresql多对多协会不起作用,node.js,postgresql,many-to-many,sails.js,waterline,Node.js,Postgresql,Many To Many,Sails.js,Waterline,我正在尝试在两个模型(操作符和组)之间创建多对多关联 有两种模式: -Operator.js var Operator = { connection:'postgresql', tableName: 'operator', schema:true, attributes: { firstName: { type: 'string', required: true, max: 64,

我正在尝试在两个模型(操作符和组)之间创建多对多关联

有两种模式:

-Operator.js

    var Operator = {
    connection:'postgresql',
    tableName: 'operator',
    schema:true,
    attributes: {

    firstName: {
        type: 'string',
        required: true,
        max: 64,
        columnName: 'first_name'
    },
    lastName: {
        type: 'string',
        required: true,
        max: 64,
        columnName: 'last_name'
    },
    birthDate: {
        type: 'date',
        columnName: 'birth_date'
    },
    sex: {
        type: 'string',
        enum: ['M', 'F', 'N.A.'],
        columnName: 'sex'
    },
    email: {
        type: 'email',
        required: true,
        columnName: 'email'
    },
    login: {
        type: 'string',
        required: true,
        max: 64
    },
    password: {
        type: 'string',
        required: true
    },

    createdAt: {
        columnName: 'created_at'
    },

    updatedAt: { 
        columnName: 'updated_at'
    },

    groups:{
        collection:'group',
        via:'operators'
    },

    // Override toJSON instance method
    // to remove password value
    toJSON: function() {
        var obj = this.toObject();
        delete obj.password;
        return obj;
    }


},

// Lifecycle Callbacks
beforeCreate: function(values, next) {
    bcrypt.hash(values.password, 10, function(err, hash) {
        if(err) return next(err);
        values.password = hash;
        next();
    });
}

};

module.exports = Operator;
.
.
.
    groups:{
        collection:'group',
        via:'id'
    },
.
.
-Group.js

    var bcrypt = require('bcrypt');

    var Group = {
    connection:'postgresql',
    tableName: 'group',
    schema:true,
    attributes: {
    name: {
        type: 'string',
        required: true,
        unique:true,
        columnName: 'name'
    },
    createdAt: {
        columnName: 'created_at'
    },
    updatedAt: {
        columnName: 'updated_at'
    },

    operators:
        {
        collection:'operator',
        via:'groups',
        dominant:true
    }
       }
 };
 module.exports = Group;
我创建了联接表组\u operators\u operator\u groups,它有两列group\u operators和operator\u groups

在sails控制台上,我尝试与以下命令关联:

     Operator.find(149).populate('groups').exec(function(e,r){
     console.log(e); 
     r[0].groups.add(19);
     console.log(r[0]); 
     r[0].save(function(err){
              console.log(err);})
     });
但结果出现了一个错误:

   null
   { groups: [],
   sex: 'F',
   email: 'nhjfhry@t5rjyi.girtj',
   login: 'tryjrtyh',
   password: '$2a$10$3pOYcOpWWcU868LBB0Gki./n9nrooXyDqSNYz1NJCkvQ480KT5uxO',
   id: 149,
   firstName: 'fgterier',
   lastName: 'gdfgjdi',
   birthDate: Thu Sep 25 2014 00:00:00 GMT+0200 (CEST),
   createdAt: null,
   updatedAt: Thu Oct 02 2014 12:45:14 GMT+0200 (CEST) }
   [ { type: 'insert',
   collection: 'group_operators__operator_groups',
   criteria: { group_operators: 19, operator_groups: 149 },
   values: { group_operators: 19, operator_groups: 149 },
   err: [Error: Trying to '.add()' an instance which already exists!] } ]

   Possibly unhandled Error: [object Object]
   at Object.Promise$_rejecter [as reject]   (/usr/lib/node_modules/sails/node_modules/waterline/node_modules/bluebird/js/main/promise.js:601:58)
at /usr/lib/node_modules/sails/node_modules/waterline/lib/waterline/model/lib/defaultMethods/save.js:154:16
at /usr/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:454:17
at /usr/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:444:17
at Array.forEach (native)
at _each (/usr/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:46:24)
at Object.taskComplete (/usr/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:443:13)
at processImmediate [as _immediateCallback] (timers.js:345:15)
如果我没有创建联接表并尝试进行关联,sails控制台将返回以下错误:

   Error (E_UNKNOWN) :: Encountered an unexpected error
   error: relation "group_operators__operator_groups" does not exist
   at Connection.parseE (/home/valentina/workspace/xtens-app/node_modules/sails-      postgresql/node_modules/pg.js/lib/connection.js:534:11)
at Connection.parseMessage (/home/valentina/workspace/xtens-app/node_modules/sails-postgresql/node_modules/pg.js/lib/connection.js:361:17)
at Socket.<anonymous> (/home/valentina/workspace/xtens-app/node_modules/sails-postgresql/node_modules/pg.js/lib/connection.js:105:22)
at Socket.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:764:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:426:10)
at emitReadable (_stream_readable.js:422:5)
at readableAddChunk (_stream_readable.js:165:9)
at Socket.Readable.push (_stream_readable.js:127:10)

Details:  error: relation "group_operators__operator_groups" does not exist
错误(E_未知)::遇到意外错误
错误:关系“组\运算符\运算符\组”不存在
在Connection.parseE(/home/valentina/workspace/xtens app/node_modules/sails-postgresql/node_modules/pg.js/lib/Connection.js:534:11)
在Connection.parseMessage(/home/valentina/workspace/xtens-app/node_-modules/sails-postgresql/node_-modules/pg.js/lib/Connection.js:361:17)
在插座上。(/home/valentina/workspace/xtens-app/node_-modules/sails-postgresql/node_-modules/pg.js/lib/connection.js:105:22)
在Socket.emit(events.js:95:17)
在插座上。(_stream_readable.js:764:14)
在Socket.emit(events.js:92:17)
在emitReadable上(_stream_readable.js:426:10)
在emitReadable(_stream_readable.js:422:5)
在readableAddChunk(_stream_readable.js:165:9)
在Socket.Readable.push(_stream_Readable.js:127:10)
详细信息:错误:关系“组\运算符\运算符\组”不存在
我使用的是sails 0.10.5、sails postgresql^0.10.8和postgresql 9.3.4

我应该创建联接表吗? 有什么问题?联接表为空,因此关联不存在

谢谢大家!


Valentina

我认为您不能在postgresql中命名表组,因为它会与内部数据库表冲突。您可以尝试在模型中将表名重命名为“组”。如果这不起作用, 你可以试试这个

操作员模型 //Operator.js

    var Operator = {
    connection:'postgresql',
    tableName: 'operator',
    schema:true,
    attributes: {

    firstName: {
        type: 'string',
        required: true,
        max: 64,
        columnName: 'first_name'
    },
    lastName: {
        type: 'string',
        required: true,
        max: 64,
        columnName: 'last_name'
    },
    birthDate: {
        type: 'date',
        columnName: 'birth_date'
    },
    sex: {
        type: 'string',
        enum: ['M', 'F', 'N.A.'],
        columnName: 'sex'
    },
    email: {
        type: 'email',
        required: true,
        columnName: 'email'
    },
    login: {
        type: 'string',
        required: true,
        max: 64
    },
    password: {
        type: 'string',
        required: true
    },

    createdAt: {
        columnName: 'created_at'
    },

    updatedAt: { 
        columnName: 'updated_at'
    },

    groups:{
        collection:'group',
        via:'operators'
    },

    // Override toJSON instance method
    // to remove password value
    toJSON: function() {
        var obj = this.toObject();
        delete obj.password;
        return obj;
    }


},

// Lifecycle Callbacks
beforeCreate: function(values, next) {
    bcrypt.hash(values.password, 10, function(err, hash) {
        if(err) return next(err);
        values.password = hash;
        next();
    });
}

};

module.exports = Operator;
.
.
.
    groups:{
        collection:'group',
        via:'id'
    },
.
.
群模型

//Groups.js

.
.
    operators:
        {
        collection:'operator',
        via:'id',
        dominant:true
    }
.
.

这是撞帆吗?不,这不是撞帆。帆在工作。