I try to create a table using sequelize it goes fine with no error but instead of creating a table is show this message as a result
Executing (default): SELECT 1+1 AS result
here my config file:
const Sequelize = require('sequelize');
const sequelize = new Sequelize('*******', '******', '*******', {
dialect: 'mysql',
host: 'localhost',
});
module.exports = sequelize;
here my user.js file:
const Sequelize = require('sequelize');
const sequelize = require('../db/mysql/config');
const User = sequelize.define('user', {
id: {
type: Sequelize.INREGER,
autoIncrement: true,
allowNull: false,
primaryKey: true
},
userName: {
type: Sequelize.STRING,
allowNull: false
},
email: {
type: Sequelize.STRING,
allowNull: false
},
password: {
type: Sequelize.STRING,
allowNull: false
}
});
module.exports = User;
and this is my test.js file i run it with node mend:
const sequelize = require('./db/mysql/config')
sequelize
.sync()
.then(result => {
console.log(result);
})
.catch(err => {
console.log(err);
})
if I copy user.js code inside config file it works fine
I try to create a table using sequelize it goes fine with no error but instead of creating a table is show this message as a result
Executing (default): SELECT 1+1 AS result
here my config file:
const Sequelize = require('sequelize');
const sequelize = new Sequelize('*******', '******', '*******', {
dialect: 'mysql',
host: 'localhost',
});
module.exports = sequelize;
here my user.js file:
const Sequelize = require('sequelize');
const sequelize = require('../db/mysql/config');
const User = sequelize.define('user', {
id: {
type: Sequelize.INREGER,
autoIncrement: true,
allowNull: false,
primaryKey: true
},
userName: {
type: Sequelize.STRING,
allowNull: false
},
email: {
type: Sequelize.STRING,
allowNull: false
},
password: {
type: Sequelize.STRING,
allowNull: false
}
});
module.exports = User;
and this is my test.js file i run it with node mend:
const sequelize = require('./db/mysql/config')
sequelize
.sync()
.then(result => {
console.log(result);
})
.catch(err => {
console.log(err);
})
if I copy user.js code inside config file it works fine
Share Improve this question edited Aug 27, 2020 at 8:30 sinammd asked Aug 27, 2020 at 8:09 sinammdsinammd 1321 gold badge1 silver badge9 bronze badges8 Answers
Reset to default 5import your user.js model in config.js a example:-
const User = require('enter your path here for user.js')
User.sync();
Because you are not exporting the Tables you have created.
- In your file you have created user.js, so you have to export it and make sure you are hitting(synchronizing) that table, either you can sync a particular table, or you can sync all the tables(models) at once.
How you can sync a Particular table (model)?
- In your root file(which starts the application), you import the user.js
import user.js from ('your path to the file')
- Now write modelName.sync();
How you can hit(sync) all the tables at once?
- After you created all the models, you have to export it
- Then in your root file, write the following
sequelize.sync()
once you import your model and require it into your controller, and then define your route, finishing by adding the route to the index.js or your app's entry point, the table will be created automatically, and no need to require the model directly into your entry point file meaning;
- yourModel.js [export your model]
- yourController.js [require your model here]
- yourRoutes.js [require your controller]
- index.js | app.js | main.js [require your router]
const sequelize = require('./db/mysql/config')
sequelize
.sync()
.then(result => {
console.log(result);
})
.catch(err => {
console.log(err);
})
the path should be changed
const sequelize = require('./models/user')
import user.js not db-mysql-config.js
Importing user.js file to test.js solved the issue for me.
You need to execute your models somehow to create tables. You can do it by requiring your model files in a program execution flow.
My solution process : => Check database OWNER before
- Save all models files in
/app/models
- Model example :
const {DataTypes, Model} = require('sequelize');
// on importe la connexion
const sequelize = require('./../database');
class User extends Model {
}
User.init({
username: {
type: DataTypes.STRING,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
password: {
type: DataTypes.STRING,
allowNull: false
},
roles: {
type: DataTypes.JSON,
defaultValue: {"roles":[{
"role_name":"ROLE_USER",
}
]},
},
status: {
type: DataTypes.BOOLEAN,
defaultValue: false
},
birthday: {
type: DataTypes.DATE,
allowNull: false
},
filename: DataTypes.STRING,
activation_token: DataTypes.STRING,
},
{
underscored: true,
sequelize, //connexion a l'instance
timestamps: true,
tableName: 'user'
}
);
User.sync({ alter: true });
console.log("==> The table for the User model was just (re)created!");
module.exports = User;
- Create dedicated file for database connection in
/app/database.js
const {Sequelize} = require('sequelize');
const sequelize = new Sequelize(process.env.PG_URL, {});
module.exports = sequelize;
- Create inside directory
/app/models
new fileindex.js
- Inside this file add all models required like that :
const User = require('./user');
const Category = require('./category');
...
module.exports = { User, Category };
- Create new file on project root like :
init_models_db.js
const models = require('./app/models'); //By default load index.js inside /app/models
const sequelize = require('./app/database');
const init_BDD = async () => {
try {
await sequelize.authenticate();
console.log('Connection has been established successfully.');
const created = sequelize.sync({force: true});
if(created) {
console.log("==> TABLE DONE !");
}
} catch (error) {
console.error('Unable to connect to the database:', error);
}
}
init_BDD();
- Execute mand :
node init_models_db.js
const Sequelize = require('sequelize');
const sequelize = new Sequelize('*******', '******', '*******', {
dialect: 'mysql',
host: 'localhost',
logging: false
});
module.exports = sequelize;
You need to add 'logging: false'
which solves the problem
Easy fix: Go to your controller file and instantiate your object
const User = require('models/user');
As soon as you create an instance for your model the tables will be generated.