I'm learning node.js and sequelizer and I encountered a problem, i.e. I created a User model using Sequelizer, unfortunately when i want to run in index.js sequelizer.sync (); it in the console shows me the result, but it does not create a table.
My model:
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const User = sequelize.define('user', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true
},
name: {
type: Sequelize.TEXT,
allowNull: false
},
surname: {
type: Sequelize.TEXT,
allowNull: false
}
});
module.exports = User;
my database config
const Sequelize = require('sequelize');
const sequelize = new Sequelize('node-nauka', 'root', '', {
dialect: 'mysql',
host: 'localhost'
});
module.exports = sequelize;
index.js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const defaultRoutes = require('./routes/default');
const sequelize = require('./util/database');
app.use(bodyParser.json());
app.use(defaultRoutes);
sequelize.sync().then(result => {
console.log(result);
app.listen(3000);
}).catch(err => {
console.log(err);
});
Sync result: https://pastebin.com/rtxqkMtx I miss ideas, tried on several databases and the same effect, no error but also does not create a table.
In my case i just import Model to app.js
const sequelize = require("./src/Utils/database");
const User = require("./src/models/User");
sequelize
.sync()
.then((result) => {
console.log(result);
})
.catch((err) => {
console.log(err);
});
@PawelC your code is same as mine so it will also work for you
Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With