Huibin Wu
Huibin Wu

Reputation: 81

Sequelize could not create a new table

I'm currently learning sequelize and its define method is used to create new database table, but it is not working...there is no error...do you know whats going on?

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: '[email protected]'
}).then(function(result){
    console.log('inserted XiaoMing ok');
}).catch(function(err){
    console.log('inserted XiaoMing error');
    console.log(err.message);
});

module.exports=router;

It says the user table doesnt exist....

Upvotes: 0

Views: 5003

Answers (1)

Vivek Doshi
Vivek Doshi

Reputation: 58523

Okay ,

First check in the database if user table is there or not , coz the below code won't create a new table , it will just create a new entry in to user table

User.create({
    name: 'XiaoMing',
    password: '1234567890',
    mail: '[email protected]'
})

If you want to create a table then you can use ,

// 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
});

I hope this will clear all your doubts

Upvotes: 1

Related Questions