SequelizeDatabaseError:关系用户不存在

时间:2018-05-23 05:51:37

标签: node.js postgresql sequelize.js

我是nodejs的新手。我在项目中使用了一些模块。 Express,sequelize,body parser,http。我有两个模型,用户和订单。但是当我运行app时抛出一个错误。也许名字很多,我不是。

index.js

const express = require('express');
const http = require('http');
const bodyParser = require('body-parser');
const app = express();
const config = require('./config');
const router = require('./router');
const env = process.env.NODE_ENV || 'development';

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static('client'));

router.set(app);

app.listen(config.port, () => console.log('App listening on port ' + config.port));

模型/ index.js

const Sequelize = require('sequelize');
const sequelize = require('../db');
const User = sequelize.define('user', {
login: Sequelize.STRING,
password: Sequelize.STRING,
});

const Order = sequelize.define('order', {
title: Sequelize.STRING,
date: {
    type: Sequelize.DATE,
    defaultValue: Sequelize.NOW
},
user_id: {
    type: Sequelize.INTEGER,
    references: {
        model: User,
        key: "id"
    }
}
});

User.sync({ force: true }).then(() => {});
Order.sync({ force: true }).then(() => {});
User.hasMany(Order, { foreignKey: 'user_id', onDelete: 'CASCADE' });

module.exports = {
User,
Order
}

db.js

const config = require('./config');
const Sequelize = require('sequelize');

const sequelize = new Sequelize('postgres://postgres:postgres@localhost:5432/zablet');
require('sequelize-values')(sequelize);

module.exports = sequelize;

config.js

module.exports = {
port: 3000,
dbConnectionString: 'postgres://postgres:postgres@localhost:5432/test',
saltRounds: 2,
jwtSecret: 'thesecretkey,
tokenExpireTime: '6h'
}

我认为我的模型存在问题。为什么关系没有定义?

0 个答案:

没有答案