Cannot find module 'sequelize/types' - mysql

anyone knows why i am getting this error
this is my code
"use strict";
const { DataTypes } = require("sequelize/types");
module.exports = {
up: async (queryInterface, DataTypes) => {
await queryInterface.createTable("dummytables", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
id: {
type: DataTypes.NUMBER,
},
first_name: {
type: DataTypes.STRING,
allowNull: false,
},
last_name: {
type: DataTypes.STRING,
allowNull: false,
},
});
},
down: async (queryInterface, DataTypes) => {
await queryInterface.dropTable("dummytables");
},
};
when am trying to run this command sequelize db:migrate
and its showing me ERROR: Cannot find module 'sequelize/types'
my dependencies file
"dependencies": {
"#types/sequelize": "^4.28.9",
"express": "^4.17.1",
"mysql2": "^2.2.5",
"sequelize": "^6.5.0",
"sequelize-cli": "^6.2.0" }
any solution need help

"use strict";
//const { DataTypes } = require("sequelize/types"); // Remove this line
module.exports = {
up: async (queryInterface, DataTypes) => {
await queryInterface.createTable("dummytables", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
id: {
type: DataTypes.NUMBER,
},
first_name: {
type: DataTypes.STRING,
allowNull: false,
},
last_name: {
type: DataTypes.STRING,
allowNull: false,
},
});
},
down: async (queryInterface, DataTypes) => {
await queryInterface.dropTable("dummytables");
},
};

If you change the second line to;
const { DataTypes } = require("sequelize");
It should work fine.

A better fix would be to just install Sequelize like;
const Sequelize = require("sequelize");
then use it like;
first_name: {
type: Sequelize.STRING,
allowNull: false,
},
You might not even need to do the importation because up() would give it to you for free, You just need to replace DataTypes with Sequelize.
async up(queryInterface, Sequelize) {
...
}

This error, may be can happen when you use auto import and it import sequelize/types, you can find in code has 'sequelize/types' and delete it in code of you should change
const {DataTypes} = require('sequelize');

Related

TypeError: models.leaves.getAllEmpoyees is not a function

I am writing a function in Schema Model for getting Data with a query. The query is working fine, but unfortunately i am getting an error regarding Function - getAllEmpoyees() not found. For Reference - my sequelize version is - 6.15.0 . I am new to Node.js. Can anyone help me out yrr, Thanks in Advance!
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('leaves', {
id: {
autoIncrement: true,
type: DataTypes.BIGINT.UNSIGNED,
allowNull: false,
primaryKey: true
},
employee_id: {
type: DataTypes.INTEGER,
allowNull: false
},
leave_type_id: {
type: DataTypes.INTEGER,
allowNull: false
},
leave_reason: {
type: DataTypes.STRING(255),
allowNull: false
},
remark: {
type: DataTypes.STRING(255),
},
status: {
type: DataTypes.STRING(255),
allowNull: false
},
created_by: {
type: DataTypes.INTEGER
}
},
{
sequelize,
tableName: 'leaves',
timestamps: true,
indexes: [
{
name: "PRIMARY",
unique: true,
using: "BTREE",
fields: [
{ name: "id" },
]
},
]
},
getAllEmpoyees = function() {
var query = "select * from leaves as l join leave_types as lt on l.leave_type_id=lt.id";
return sequelize.query(query, { type: sequelize.QueryTypes.SELECT});
},
);
};
const express = require('express');
const router = express.Router();
var path = require('path');
var root_path = path.dirname(require.main.filename);
var models = require(root_path + '/models');
var moment = require("moment");
router.get('/getallemployeeLeaves', (req, res) => {
console.log("All Fetched");
models.leaves.getAllEmpoyees().then(function (data) {
console.log("");
if (data.length > 0) {
res.json({
status: 200,
data: data
})
} else {
res.json({
status: 400,
})
}
})
})

Sequelize UUID with mySQL: Unable to set default value

I've been trying for awhile to set id (primary key) for my Users table as UUID. However, I keep getting this error: Field 'id' doesn't have a default value, when I attempt to seed it.
This is what I have so far in my Users model:
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Users extends Model {};
Users.init({
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true
},
user_name: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: {
msg: 'Please add a name',
},
},
},
{
sequelize,
modelName: 'Users',
});
return Users;
Likewise, this is what I have in my Users migration file:
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('Admins', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true
},
user_name: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Users');
}
};
I'm pretty new to Sequelize, so would love some guidance on what's gone wrong!
By adding defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), to your createdAt and updatedAt in your migration file defaults the value to the current timestamp.
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
So, I think I realised the issue:
For some reason, seeding it by using a seeder file would not auto-generate the fields that I thought would be auto-generated, so I had to put them in manually.
'use strict';
const { v4: uuidv4 } = require('uuid');
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Users', [{
id: uuidv4(),
user_name: 'John Doe',
"createdAt": new Date(),
"updatedAt": new Date()
}], {});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Users', null, {});
}
};
Initially, I had been trying to seed (running the command npx sequelize-cli db:seed:all) without the id: uuidv4() and the new Date(), which was why it didn't work.

Sequelize MySQL Migration - unique to false

I am trying to write a migration in Sequelize and MySQL, which sets the unique attribute to false. This is my approach so far:
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.changeColumn("users", "name", {
type: Sequelize.STRING,
allowNull: false,
unique: true,
});
await queryInterface.changeColumn("users", "email", {
type: Sequelize.STRING,
allowNull: false,
unique: true,
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.changeColumn("users", "name", {
type: Sequelize.STRING,
allowNull: true,
unique: false,
});
await queryInterface.changeColumn("users", "email", {
type: Sequelize.STRING,
allowNull: true,
unique: false,
});
},
};
The up migration works like charme, the down migration works for allowNull, but not for unique attribute. I am new to Sequelize so I am wondering, what is going wrong here. Can someone help me out?
Thank you very much in advance.
Sequelize has "removeConstraint" method, using which you will be able to remove the constraint for the attribute.
public removeConstraint(tableName: string, constraintName: string, options: Object): Promise
Actually, if you don't specify the "constraintName" in creating, it should be of the form "attributename_unique_key".
Please try using this way
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.changeColumn("users", "name", {
type: Sequelize.STRING,
allowNull: false,
unique: true,
});
await queryInterface.changeColumn("users", "email", {
type: Sequelize.STRING,
allowNull: false,
unique: true,
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.changeColumn("users", "name", {
type: Sequelize.STRING,
allowNull: true,
});
await queryInterface.removeConstraint("users", "name_unique_key");
await queryInterface.changeColumn("users", "email", {
type: Sequelize.STRING,
allowNull: true,
});
await queryInterface.removeConstraint("users", "email_unique_key");
},
};
For more information please check the documentation here

ERROR: Cannot find module 'sequelize/types'

C:\Users\lenovo\Desktop\Yoobou\Yoobou>sequelize db:migrate
Sequelize CLI [Node: 14.15.1, CLI: 6.2.0, ORM: 6.3.5]
Loaded configuration file "config\config.json". Using environment
"development".
== 20201207141344-create-producteurs: migrating =======
ERROR: Cannot find module 'sequelize/types' Require stack:
C:\Users\lenovo\Desktop\Yoobou\Yoobou\migrations\20201207141344-create-producteurs.js
C:\Users\lenovo\AppData\Roaming\npm\node_modules\sequelize-cli\node_modules\umzug\lib\migration.js
C:\Users\lenovo\AppData\Roaming\npm\node_modules\sequelize-cli\node_modules\umzug\lib\index.js
C:\Users\lenovo\AppData\Roaming\npm\node_modules\sequelize-cli\lib\core\migrator.js
C:\Users\lenovo\AppData\Roaming\npm\node_modules\sequelize-cli\lib\commands\migrate.js
C:\Users\lenovo\AppData\Roaming\npm\node_modules\sequelize-cli\lib\sequelize
//MIGRATION 20201207141344-create-producteurs.js
'use strict'; const { UniqueConstraintError } =
require('sequelize/types');
module.exports = { up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('PRODUCTEURS', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
first_name: {
allowNull: false,
type: Sequelize.STRING,
unique: true,
},
last_name: {
allowNull: false,
type: Sequelize.STRING,
},
email: {
allowNull: false,
type: Sequelize.STRING,
Unique: true,
},
password: {
allowNull: false,
type: Sequelize.STRING,
},
avatar: {
allowNull: false,
type: Sequelize.STRING,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
},
}); }, down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('PRODUCTEURS'); }, };
// ASSOCIATION MODELS 'use strict'; const { Model } = require('sequelize'); module.exports = (sequelize, DataTypes) => {
class ADMINISTRATEUR extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The models/index file will call this method automatically.
/
associate(models) {
// define association here
models.ADMINISTRATEUR.hasMany(models.CLIENTS);
models.ADMINISTRATEUR.hasMany(models.PRODUITS);
models.ADMINISTRATEUR.hasMany(models.ADRESSE_CLIENTS);
models.ADMINISTRATEUR.hasMany(models.CATEGORY_PRODUITS);
models.ADMINISTRATEUR.hasMany(models.COMMANDES);
models.ADMINISTRATEUR.hasMany(models.PRODUCTEURS);
models.ADMINISTRATEUR.hasMany(models.AVIS);
} } ADMINISTRATEUR.init(
{
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
email: DataTypes.STRING,
password: DataTypes.STRING,
avatar: DataTypes.STRING,
},
{
sequelize,
modelName: 'ADMINISTRATEUR',
} ); return ADMINISTRATEUR; }; 'use strict'; const { Model } = require('sequelize'); module.exports = (sequelize, DataTypes) =>
{ class PRODUCTEURS extends Model {
/*
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The models/index file will call this method automatically.
*/
static associate(models) {
// define association here
models.PRODUCTEURS.belongsTo(models.ADMINISTRATEUR , {
foreignKey: {
allowNull: false
}
});
models.PRODUCTEURS.hasMany(models.CLIENTS);
models.PRODUCTEURS.hasMany(models.PRODUITS);
models.PRODUCTEURS.hasMany(models.ADRESSE_CLIENTS);
models.PRODUCTEURS.hasMany(models.CATEGORY_PRODUITS);
models.PRODUCTEURS.hasMany(models.COMMANDES);
} }; PRODUCTEURS.init({
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
email: DataTypes.STRING,
password: DataTypes.STRING,
avatar: DataTypes.STRING }, {
sequelize,
modelName: 'PRODUCTEURS', }); return PRODUCTEURS; };
I finally found the answer I had to put the variable "const {UniqueConstraintError} = require ('sequelize / types')" in comment and retype sequelize db: migrate

why model not fetching all attributes in table in sequelize

I have created model for my databse and then run migration it successfully created the table in database after this I create migration to add column to that existing table . When I run model.findall query it only gets the attributes that I created first time e.g here is my model file
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('ActiveUsers', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
username: {
type: Sequelize.STRING
},
name: {
type: Sequelize.STRING
},
socketId: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('ActiveUsers');
}
};
here is migration file to add column to this table
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
queryInterface.addColumn(
'ActiveUsers',
'Token',
{
type: Sequelize.STRING,
allowNull: false
}
)
},
down: (queryInterface, Sequelize) => {
}
};
here is table pic
it only gets the attributes that are present in model file i.e
username,name,socketId,updatedAt,createdAt
why it dont get the value of
token,status
here is my code
activeusers.findAll({raw:true}).then(Users=>{
console.log('online users')
})
The first file you wrote is not a model file, it is a migration file. If you want to select your new fields you should add them to your model file.
Your model file should look like this:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('activeUsers', {
id: {
type: DataTypes.STRING,
allowNull: false,
primaryKey: true,
unique: true
},
username: {
type: Sequelize.STRING
},
name: {
type: Sequelize.STRING
},
socketId: {
type: Sequelize.STRING
},
token: {
type: Sequelize.STRING
},
status: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
};
You can read more in Sequelize docs about how to add models to your project.
We have to add column fields to model file manually . then it will fetch that fields