2

I'm learning node.js and mySQL. I have tried Sequelize and according to my learning source sync should create new table if doesn't exist. But for some reason it doesn't create new table.

Here is my database.js file

const Sequelize = require('sequelize');

const sequelize = new Sequelize('test-schema', 'root', 'mypassword',{dialect:'mysql', host:'localhost'});

module.exports = sequelize;

Here is my model Product file

const Sequelize = require('sequelize');

const sequelize = require('../util/database')

const Product = sequelize.define('product', {
    id: {type: Sequelize.INTEGER, autoIncrement: true, allowNull: false, primaryKey: true},
    title: Sequelize.STRING,
    price: {type: Sequelize.DOUBLE, allowNull: false}
});

module.exports = Product;

and here is my server.js file

const express = require('express');

const sequelize = require('./util/database')

const app = express();


app.get('/',(req,res,next)=>{
    res.send({"id": "1"});
});

sequelize.sync().then(result=>{
    //console.log(result);
    app.listen(3000);
}).catch(err => {
    console.log(err);
});

Once I start server I get

Executing (default): SELECT 1+1 AS result

I tried to change the name of my schema in my database file to wrong name and I get an error schema doesn't exist so I believe that connection to db is correct

Here are my installed packages

  "dependencies": {
    "express": "^4.17.1",
    "mysql2": "^2.1.0",
    "sequelize": "^5.21.10"
  }
delmin
  • 2,330
  • 5
  • 28
  • 54
  • Where you you import the Product file? – Anatoly May 19 '20 at 17:57
  • @Anatoly I don't import it.. Sequelize should go through all models in `sequelize.define` and create tables if they don't exist according to my learning source – delmin May 19 '20 at 18:03
  • How sequelize know about a model in a JS-file that you never import? – Anatoly May 19 '20 at 18:23
  • @Anatoly database is created in sequelizer which is connectect with model (Product) through `sequelize.define` which also run on start in server.js in `sequelize.sync()` This is my first time working with sequelizer and I've checked my code 3 times with my source and didn't notice any mistake. Do you think it should be imported anyway? If so then where? I don't see any point importing it – delmin May 19 '20 at 18:29

2 Answers2

2

Define all models as functions so that you can call them to register models in sequelize and then register models in database.js just like I described in this answer. You can see in the question of this answer how to define a model like a function.

'use strict';
module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    id: { type: DataTypes.BIGINT, allowNull: false, autoIncrement: true, unique: true, primaryKey: true },
    first_name: DataTypes.STRING,
    last_name: DataTypes.STRING
  }, {});
  User.associate = function(models) {
    User.belongsTo(models.Role, { foreignKey: 'role_id' });
  };
  return User;
};
Anatoly
  • 20,799
  • 3
  • 28
  • 42
  • 2
    Thanks Anatoly... I have just create import in my server.js with my product file `const product = require('./models/products')` and it start to work. I don't get it. I really don't get it... That import or constant is not being used anywhere in server.js. It is just an unused import. I'm gonna look at your solution now.. – delmin May 19 '20 at 18:45
  • 1
    hmm.. It will take me some times to go through your solution (I'm beginner in node.js) but I believe that your solution is correct.. I'm still trying to figure out the logic behind that unused import so I can not concentrate on other things now... Thank you very very much – delmin May 19 '20 at 18:53
0

In your Product file, change that p after sequelize.define from small letter to capital letter. It should be "Product" not "product". It should look like:

const Product = sequelize.define('Product', {
    id: {type: Sequelize.INTEGER, autoIncrement: true, allowNull: false, primaryKey: true},
    title: Sequelize.STRING,
    price: {type: Sequelize.DOUBLE, allowNull: false}
});

module.exports = Product;
Tyler2P
  • 2,324
  • 26
  • 22
  • 31