2017-12-28 18 views
0

필자는 sequelize가 새로 생겼다. 작업 관계가 null 인 내 사용자 테이블에서 모든 항목을로드하려고한다. 하지만 작동하지 않습니다. 나는 세 명의 사용자가있는 경우후속 작업에서 관계가 null 인 항목로드

const express = require('express'); 
const app = express(); 

const Sequelize = require('sequelize'); 
const sequelize = new Sequelize('sequelize', 'mazinoukah', 'solomon1', { 
    host: 'localhost', 
    dialect: 'postgres', 

    pool: { 
    max: 5, 
    min: 0, 
    acquire: 30000, 
    idle: 10000, 
    }, 
}); 

const Task = sequelize.define('Task', { 
    name: Sequelize.STRING, 
    completed: Sequelize.BOOLEAN, 
    UserId: { 
    type: Sequelize.INTEGER, 
    references: { 
     model: 'Users', // Can be both a string representing the table name, or a reference to the model 
     key: 'id', 
    }, 
    }, 
}); 

const User = sequelize.define('User', { 
    firstName: Sequelize.STRING, 
    lastName: Sequelize.STRING, 
    email: Sequelize.STRING, 
    TaskId: { 
    type: Sequelize.INTEGER, 
    references: { 
     model: 'Tasks', // Can be both a string representing the table name, or a reference to the model 
     key: 'id', 
    }, 
    }, 
}); 

User.hasOne(Task); 
Task.belongsTo(User); 

app.get('/users', (req, res) => { 
    User.findAll({ 
    where: { 
     Task: { 
     [Sequelize.Op.eq]: null, 
     }, 
    }, 
    include: [ 
     { 
     model: Task, 
     }, 
    ], 
    }).then(function(todo) { 
    res.json(todo); 
    }); 
}); 

    app.listen(2000,() => { 
     console.log('server started'); 
    }); 

, 그 사용자의 2는 내가 작업없이 바로 최종 사용자를로드 할, 각 작업이 : 여기 내가 시도한 것입니다. 이것이 후편에 가능합니까?

답변

1

많은 디버깅 후 나는 절

where: { 
    '$Task$': null, 
}, 

내가 작업없이 사용자 만로드 할 수 있었던 곳을 추가하여 솔루션

app.get('/users', (req, res) => { 
User.findAll({ 
    where: { 
     '$Task$': null, 
    }, 
    include: [ 
     { 
     model: Task, 
     required: false, 
     }, 
    ], 
    }).then(function(todo) { 
    res.json(todo); 
    }); 
}); 

을 발견