最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

在sequelize中加载关系为null的项目

IT培训 admin 5浏览 0评论

在sequelize中加载关系为null的项目

我是sequelize的新手,我正在尝试加载任务关系为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个用户各有一个任务,我想加载没有任务的最后一个用户。这可能是续集吗?

回答如下:

经过多次调试后我找到了解决方案

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

通过添加这个where子句

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

我只能加载没有任务的用户

在sequelize中加载关系为null的项目

我是sequelize的新手,我正在尝试加载任务关系为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个用户各有一个任务,我想加载没有任务的最后一个用户。这可能是续集吗?

回答如下:

经过多次调试后我找到了解决方案

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

通过添加这个where子句

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

我只能加载没有任务的用户

发布评论

评论列表 (0)

  1. 暂无评论