Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize sync and Node.js not create table

Tags:

node.js

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.

like image 682
PawelC Avatar asked Dec 05 '22 10:12

PawelC


1 Answers

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

like image 174
Niket Tongare Avatar answered Dec 30 '22 21:12

Niket Tongare