Sequelizejs:未处理的拒绝SequelizeDatabaseError:列user.playerstatsId不存在

时间:2018-05-05 03:17:45

标签: sequelize.js

我有一个现有的数据库,我想从表“User”中检索,但是用户表与“PlayerStats”表有一对一的关系。我想检索User对象,它也是Playerstats作为Json对象而不是外键Id.After编写我的代码并运行它后,我在控制台中收到错误说:

    Sat, 05 May 2018 02:18:26 GMT sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators at node_modules\sequelize\lib\sequelize.js:242:13
Executing (default): SELECT "user"."id", "user"."address", "user"."bio", "user"."country", "user"."email", "user"."firstname", "user"."gender", "user"."lastname", "user"."locked", "user"."money", "user"."onlinestatus", "user"."password", "user"."phonenumber", "user"."picture", "user"."username", "user"."usertype", "user"."playerstatsId", "playerstats"."id" AS "playerstats.id", "playerstats"."time" AS "playerstats.time", "playerstats"."expenses" AS "playerstats.expenses", "playerstats"."friends" AS "playerstats.friends", "playerstats"."loss" AS "playerstats.loss", "playerstats"."matchplayed" AS "playerstats.matchplayed", "playerstats"."profit" AS "playerstats.profit", "playerstats"."readmessage" AS "playerstats.readmessage", "playerstats"."referrals" AS "playerstats.referrals", "playerstats"."referrer" AS "playerstats.referrer", "playerstats"."totalmessage" AS "playerstats.totalmessage", "playerstats"."unreadmessage" AS "playerstats.unreadmessage", "playerstats"."wallet" AS "playerstats.wallet", "playerstats"."wins" AS "playerstats.wins" FROM "users" AS "user" LEFT OUTER JOIN "playerstats" AS "playerstats" ON "user"."playerstatsId" = "playerstats"."id" LIMIT 1;
Unhandled rejection SequelizeDatabaseError: column user.playerstatsId does not exist

根据我的理解,我觉得sequelize试图加入Users和Playerstats表。

这是我的用户代码:

  var Sequelize = require('sequelize');

var db = require('../database/postgres');


var User =db.define('user', {

    id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        primaryKey: true
    },

    address: {
        type: Sequelize.STRING,
        field: 'address'
    },

    bio: {
        type: Sequelize.STRING,
        field: 'bio'
    },

    country: {
        type: Sequelize.STRING,
        field: 'country'
    },

    email: {
        type: Sequelize.STRING,
        field: 'email'
    },

    firstname: {
        type: Sequelize.STRING,
        field: 'firstname'
    },

    gender: {
        type: Sequelize.STRING,
        field: 'gender'
    },

    lastname: {
        type: Sequelize.STRING,
        field: 'lastname'
    },

    locked: {
        type: Sequelize.BOOLEAN,
        field: 'locked'
    },

    money: {
        type: Sequelize.DOUBLE,
        field: 'money'
    },

    onlinestatus: {
        type: Sequelize.STRING,
        field: 'onlinestatus'
    },

    password: {
        type: Sequelize.STRING,
        field: 'password'
    },

    phonenumber: {
        type: Sequelize.STRING,
        field: 'phonenumber'
    },

    picture: {
        type: Sequelize.STRING,
        field: 'picture'
    },

    username: {
        type: Sequelize.STRING,
        field: 'username'
    },

    usertype: {
        type: Sequelize.STRING,
        field: 'usertype'
    },



}, {
    tableName: 'users',

    timestamps: false
});


module.exports = User;

我的PlayerStats代码:

    var Sequelize = require('sequelize');

var db = require('../database/postgres');


var PlayerStats =db.define('playerstats', {

    id: {
        type: Sequelize.INTEGER,
        field: 'id',
        allowNull: false,
        primaryKey: true
    },

    time: {
        type: Sequelize.DATE,
        field: 'time'
    },

    expenses: {
        type: Sequelize.DOUBLE,
        field: 'expenses'
    },

    friends: {
        type: Sequelize.INTEGER,
        field: 'friends'
    },

    loss: {
        type: Sequelize.INTEGER,
        field: 'loss'
    },

    matchplayed: {
        type: Sequelize.INTEGER,
        field: 'matchplayed'
    },

    profit: {
        type: Sequelize.DOUBLE,
        field: 'profit'
    },

    readmessage: {
        type: Sequelize.INTEGER,
        field: 'readmessage'
    },

    referrals: {
        type: Sequelize.INTEGER,
        field: 'referrals'
    },

    referrer: {
        type: Sequelize.INTEGER,
        field: 'referrer'
    },

    totalmessage: {
        type: Sequelize.INTEGER,
        field: 'totalmessage'
    },

    unreadmessage: {
        type: Sequelize.INTEGER,
        field: 'unreadmessage'
    },

    wallet: {
        type: Sequelize.DOUBLE,
        field: 'wallet'
    },

    wins: {
        type: Sequelize.INTEGER,
        field: 'wins'
    }

}, {
    tableName: 'playerstats',

    timestamps: false
});


module.exports = PlayerStats;

运行它们的最后一个主要的js类:

    var io = require('socket.io')(process.env.PORT || 4000);

var User = require('./server/entities/user');

var PlayerStats = require('./server/entities/playerstats');





new Server();

const room = 'quick-chat';

function Server() {


    User.belongsTo(PlayerStats,{as: 'playerstats'});


    User.findOne({include: [{

        model: PlayerStats,

        as: 'playerstats'

    }]}).then(function (user) {

        console.log(user);

    });


    var global = this;



    io.on('connection',function(socket){

        console.log("A client has connected");

        socket.on('join', (data) => {

            //todo connect to the gambeat db and update the users info to online

            socket.join(room);

            console.log("client has joined");

        });

        socket.on('disconnect', () => {

            console.log("client has disconnect");

        });

    });



}

有些人可以告诉我如何去做。

1 个答案:

答案 0 :(得分:1)

我根据这个article

找出了问题
  

默认情况下,将从目标模型名称和目标主键名称生成belongsTo关系的外键。   默认大小写是camelCase,但是如果源模型配置了underscored:true,则foreignKey将为snake_case。

这导致" playerstatsId"这是我在用户表中没有的。您可以通过使用外键选项指定外键的正确名称来更正此问题。因此

User.belongsTo(PlayerStats,{foreignKey: 'playerstats',as: 'playerstat'});