Reputation: 132
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
Upvotes: 4
Views: 13517
Reputation: 31
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
Upvotes: 1
Reputation: 1
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.
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 469
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;
Upvotes: 1
Reputation: 157
My solution process : => Check database OWNER before
/app/models
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;
/app/database.js
const {Sequelize} = require('sequelize');
const sequelize = new Sequelize(process.env.PG_URL, {});
module.exports = sequelize;
/app/models
new file index.js
const User = require('./user');
const Category = require('./category');
...
module.exports = { User, Category };
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();
node init_models_db.js
Upvotes: 0
Reputation: 19
Because you are not exporting the Tables you have created.
How you can sync a Particular table (model)?
import user.js from ('your path to the file')
How you can hit(sync) all the tables at once?
sequelize.sync()
Upvotes: 1
Reputation: 33
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.
Upvotes: 0
Reputation: 66
import your user.js model in config.js a example:-
const User = require('enter your path here for user.js')
User.sync();
Upvotes: 5