我目前正在学习sequelize,它的define方法用于创建新的数据库表,但它不起作用......没有错误......你知道发生了什么事吗?
var express = require('express');
var Sequelize = require('sequelize');
var sequelize = require('../db/sequelize_conf.js');
var router = express.Router();
var User = sequelize.define('user',
{
name: Sequelize.STRING,
password: Sequelize.STRING,
mail: Sequelize.STRING
},
{
freezeTableName: true,
timestamps: false
});
User.create({
name: 'XiaoMing',
password: '1234567890',
mail: 'xiaoming@qq.com'
}).then(function(result){
console.log('inserted XiaoMing ok');
}).catch(function(err){
console.log('inserted XiaoMing error');
console.log(err.message);
});
module.exports=router;
它说用户表不存在....
答案 0 :(得分:1)
好的,
首先检查数据库中是否有用户表,因为下面的代码不会创建新表,它将只是在用户表中创建一个新条目
User.create({
name: 'XiaoMing',
password: '1234567890',
mail: 'xiaoming@qq.com'
})
如果你想要创建一个表,你可以使用,
// This will create only all the tables defines via sequelize
sequelize.sync().then(() =>
// put your user create code inside this
);
// OR
// This will create only user table
User.sync().then(() => {
// put your user create code inside this
});
我希望这能清除你所有的疑虑