'sequelize does not creating a table shows this result : "Executing (default): SELECT 1+1 AS result"
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 commend:
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
Solution 1:[1]
import your user.js model in config.js a example:-
const User = require('enter your path here for user.js')
User.sync();
Solution 2:[2]
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.
Solution 3:[3]
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()
Solution 4:[4]
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 command :
node init_models_db.js
Solution 5:[5]
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]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ahmed Shafraz |
Solution 2 | |
Solution 3 | Mohammed Aliyaan |
Solution 4 | |
Solution 5 | Hicham Mounadi |