I have to models labor and room_capacity, every may have different capacity 4,5,8,10
second model is rooms
I need to do association between rooms in both models to get room capacity while findAll()
the association and model.findAll required.
the first model :
module.exports = (sequelize, DataTypes) => {
const haramain = sequelize.define("haramain", {
emp_no: {
type: DataTypes.BIGINT,
allowNull: false,
},
name: {
type: DataTypes.STRING,
allowNull: false
},
zoon_no: {
type: DataTypes.INTEGER,
allowNull: false
},
room_no: {
type: DataTypes.INTEGER,
allowNull: false
},
room: {
type:DataTypes.STRING,
get() {
function zeroPad(num, places) {
let zero = places - num.toLocaleString('EG').length + 1;
return Array(+(zero > 0 && zero)).join("0") + num;
}
const room = this.getDataValue('room_no')
const zoon = this.getDataValue('zoon_no')
return `Z${zoon}-${zeroPad(room,3)}`
}
},
profession: {
type: DataTypes.STRING,
allowNull: true
},
nationality: {
type: DataTypes.STRING,
allowNull: false
},
project: {
type: DataTypes.STRING,
allowNull: false
},
iqama_no: {
type: DataTypes.BIGINT,
allowNull: false
},
passport_no: {
type: DataTypes.STRING,
allowNull: true
},
in_date: {
type: DataTypes.DATEONLY,
allowNull: false,
},
emp_photo: {
type: DataTypes.BLOB,
allowNull: true
},
iqama_photo: {
type: DataTypes.BLOB,
allowNull: true
},
in_reason: {
type: DataTypes.STRING,
allowNull: false
},
out_date: {
type: DataTypes.DATEONLY,
allowNull: true
},
});
return haramain;
};
second model:
module.exports = (sequelize, DataTypes) => {
const haramainrooms = sequelize.define("haramainrooms", {
room: {
type: DataTypes.STRING,
allowNull: false,
},
capacity: {
type: DataTypes.INTEGER,
allowNull: true
}
});
return haramainrooms;
};
I have to models labor and room_capacity, every may have different capacity 4,5,8,10
second model is rooms
I need to do association between rooms in both models to get room capacity while findAll()
the association and model.findAll required.
Related
I've an existing Postgres database. Throught sequelize-auto, I generated the models for the existing database. For example, let's look at the tc_devices table (tc_devices.js):
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('tc_devices', {
id: {
autoIncrement: true,
autoIncrementIdentity: true,
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING(128),
allowNull: false
},
uniqueid: {
type: DataTypes.STRING(128),
allowNull: false,
unique: "tc_devices_uniqueid_key"
},
lastupdate: {
type: DataTypes.DATE,
allowNull: true
},
positionid: {
type: DataTypes.INTEGER,
allowNull: true
},
groupid: {
type: DataTypes.INTEGER,
allowNull: true,
references: {
model: 'tc_groups',
key: 'id'
}
},
attributes: {
type: DataTypes.STRING(4000),
allowNull: true
},
phone: {
type: DataTypes.STRING(128),
allowNull: true
},
model: {
type: DataTypes.STRING(128),
allowNull: true
},
contact: {
type: DataTypes.STRING(512),
allowNull: true
},
category: {
type: DataTypes.STRING(128),
allowNull: true
},
disabled: {
type: DataTypes.BOOLEAN,
allowNull: true,
defaultValue: false
}
}, {
sequelize,
tableName: 'tc_devices',
schema: 'public',
timestamps: false,
indexes: [
{
name: "tc_devices_pkey",
unique: true,
fields: [
{ name: "id" },
]
},
{
name: "tc_devices_uniqueid_key",
unique: true,
fields: [
{ name: "uniqueid" },
]
},
]
});
};
In database.js, I connect to the database:
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize(database, user, password, {
host,
port,
dialect: 'postgres',
logging: false
})
async function db_connection(){
try{
await sequelize.authenticate();
console.log('Connection has been estabilished successfully.');
}catch{
console.log('Unable to connect to the database.');
}
}
db_connection()
How can I do a simple query on the tc_devices table? what should I import in database.js? in tc_devices I export function(sequelize, DataTypes)... but I can't understand how to do a query in database.js with this function...could you help me? thank you so much.
You need to register all models and their associations before executing the first query with these models. Look at my answer here.
As soon as you do it you can execute queries like this:
// assuming db already stores all model definitions
const allRecords = await db.tcDevices.findAll({})
I'm working on a REST API for the backend of a simple e-commerce app using Node JS, PostgreSQL and Sequelize, and I'm facing an issue with Sequelize when I try to add a product to the shopping cart. It returns an error "column Nan does not exist"
Initially I was using Integer for the user Id as the primary key, then I changed for UUID to better suit the purpose.
The code I'm using for the models and migrations is the following:
//User model
export default (sequelize, DataTypes) => {
const User = sequelize.define(
'User',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true,
unique: true,
},
name: {
type: DataTypes.STRING,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: {
name: 'user_email',
msg: 'A user with this email already exists.'
}
},
},
User.associate = models => {
User.hasOne(models.Cart, {
foreignKey: 'userId',
as: 'cart',
onDelete: 'cascade'
});
};
User.associate = models => {
User.hasMany(models.Order, {
foreignKey: 'userId',
as: 'orders',
onDelete: 'cascade'
});
};
return User;
};
//User migration
export const up = (queryInterface, Sequelize) =>
queryInterface.createTable('Users', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true,
unique: true,
},
name: {
allowNull: false,
type: Sequelize.STRING
},
password: Sequelize.STRING,
email: {
allowNull: false,
type: Sequelize.STRING,
unique: true
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
},
});
export const down = queryInterface => queryInterface.dropTable('Users');
Cart model
export default (sequelize, DataTypes) => {
const Cart = sequelize.define('Cart', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true,
},
userId: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false
},
cartItem: {
type: DataTypes.TEXT,
allowNull: false,
get(value) {
return JSON.parse(this.getDataValue(value));
},
set(value) {
this.setDataValue('cartItem', JSON.stringify(value));
}
}
});
Cart.associate = models => {
Cart.belongsTo(models.User, {
foreignKey: 'userId',
as: 'owner'
});
};
return Cart;
};
Cart migration
export const up = (queryInterface, Sequelize) =>
queryInterface.createTable('Carts', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
},
userId: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false
},
cartItem: {
type: Sequelize.TEXT,
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
}
});
export const down = queryInterface => queryInterface.dropTable('Carts');
Code to handle the add to cart:
addToCart() {
return this.asyncWrapper(async (req, res) => {
const { body, user } = req;
body.userId = user.id;
const cart = await this.service.addToCart(body);
this.sendResponse(res, cart, undefined, 201);
});
}
Add to cart service
async cart(userId, options = {}) {
const cart = await super.find({ userId }, options);
return cart;
}
async addToCart(data, options) {
const { userId, productId, qty } = data;
const [result] = await this.model.findOrCreate({
where: { userId: +userId },
defaults: { cartItem: new CartItem() }
});
const cartData = JSON.parse(result.dataValues.cartItem);
const cartItem = new CartItem(cartData);
const product = await ProductService.getById(productId, { plain: true });
ExceptionHandler.throwErrorIfNull(product);
const cart = cartItem.addToCart(product, qty);
result.cartItem = cart;
result.save();
return result;
}
The SQL query generated by Sequelize is the following:
SELECT "id","userId","cartItem","createdAt","updatedAt" FROM "Carts" AS "Cart" WHERE "Cart"."userId" = NaN LIMIT 1;
The goal is to use UUID as primary key in the database.
This issue started when I changed the Datatype from Integer for UUID and I can't see what is wrong with the code.
Any advice on how to solve this?
Sequelize version: "^5.21.9" with "pg": "^8.2.0" and "pg-hstore": "^2.3.3".
If you switched a data type from INTEGER to UUID you shouldn't try to convert UUID-string to a number doing where: { userId: +userId }.
Pass userId as is:
where: { userId }
The foreign key returns null when inserted using create include, but the rest of data is saved from the passed object.
Here is my transaction model:
module.exports = (sequelize, DataTypes) => {
const Transaction = sequelize.define('transactions', {
id: {
type: DataTypes.INTEGER,
allowNull: true,
primaryKey: true
},
receiptNumber: {
type: DataTypes.TEXT,
allowNull: true
},
supCustID: {
type: DataTypes.INTEGER,
allowNull: true
},
userID: {
type: DataTypes.INTEGER,
allowNull: true
},
type: {
type: DataTypes.TEXT,
allowNull: true
},
status: {
type: DataTypes.INTEGER,
allowNull: true
},
remarks: {
type: DataTypes.TEXT,
allowNull: true
},
createdAt: {
type: 'TIMESTAMP',
defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
allowNull: false
},
updatedAt: {
type: 'TIMESTAMP',
defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
allowNull: false
}
}, {
tableName: 'transactions'
});
Transaction.associate = models => {
Transaction.Order = Transaction.hasMany(models.Order, {
as: 'Orders',
foreignKey: 'transaction_id'
})
Transaction.SupCust = Transaction.belongsTo(models.SupCust, {
as: 'SupCust',
foreginKey: 'supCustID'
})
Transaction.User = Transaction.belongsTo(models.User, {
as: 'User',
foreginKey: 'userID'
})
}
return Transaction;
};
Orders Model:
/* jshint indent: 1 */
module.exports = (sequelize, DataTypes) => {
const Order = sequelize.define('orders', {
id: {
type: DataTypes.INTEGER,
allowNull: true,
primaryKey: true
},
transaction_id: {
type: DataTypes.INTEGER,
allowNull: true
},
itemID: {
type: DataTypes.TEXT,
allowNull: true
},
qty: {
type: DataTypes.INTEGER,
allowNull: true
},
itemCost: {
type: DataTypes.REAL,
allowNull: true
},
discount: {
type: DataTypes.REAL,
allowNull: true
},
totalAmount: {
type: DataTypes.REAL,
allowNull: true
}
}, {
tableName: 'orders',
timestamps: false,
hooks: {
afterValidate: (Order) => {
console.log(Order)
},
}
});
Order.associate = models => {
Order.belongsTo(models.Transaction, {
as: 'Transactions',
foreignKey: 'transaction_id'
})
Order.belongsTo(models.ItemList, {
as: 'Items',
foreignKey: 'itemID'
})
}
return Order;
};
Code to execute insert data:
return await models.Transaction
.findOne({ where: { id: values.id || -1 } })
.then(async function (obj) {
if(obj) { // update
return await obj.update(values, {individualHooks: true});
}
else { // insert
const {id, ...payload} = values
return await models.Transaction.create(payload, {
include: [{
association: models.Transaction.Order
}],
});
}
})
Results from console:
Executing (default): INSERT INTO `transactions` (`id`,`receiptNumber`,`supCustID`,`userID`,`type`,`createdAt`,`updatedAt`) VALUES ($1,$2,$3,$4,$5,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP);
Executing (default): INSERT INTO `orders` (`id`,`transaction_id`,`itemID`,`qty`,`itemCost`) VALUES ($1,$2,$3,$4,$5);
Console log from hooks on orders model:
dataValues:
{ id: null,
itemID: 1008,
itemCost: '2',
qty: '1',
transaction_id: null },
Why is this always null? what am i missing something?
Solved this by adding autoincrement property on my transaction model.
id: {
type: DataTypes.INTEGER,
allowNull: true,
primaryKey: true,
autoIncrement: true
}
I'm using sequelize along with nodejs and am having difficulties with a relationship.
I have 3 tables where (local, equipment, shed, area) where in one place I have 1 equipment, it is in a shed and belongs to an area.
The models look like this:
local:
import { Sequelize, DataTypes } from 'sequelize';
export default (sequelize, dataTypes) => {
const model = sequelize.define('local', {
id: {
type: dataTypes.STRING(200),
primaryKey: true,
allowNull: false,
required: true,
unique: true
},
name: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
idEquipment: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
idShed: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
idArea: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
situation: {
type: dataTypes.BOOLEAN,
allowNull: false,
required: true,
defaultValue: true
},
capacity: {
type: dataTypes.FLOAT,
allowNull: false,
required: true
},
posX: {
type: dataTypes.STRING,
allowNull: false,
required: true
},
posY: {
type: dataTypes.STRING,
allowNull: false,
required: true
},
posZ: {
type: dataTypes.STRING,
allowNull: false,
required: true
},
status: {
type: dataTypes.BOOLEAN,
allowNull: true,
defaultValue: true
}
}).schema('public');
model.associate = (models) => {
model.belongsTo(models.area, {
foreignKey: 'idArea'
});
model.belongsTo(models.equipment, {
foreignKey: 'idEquipment'
});
// model.belongsTo(models.shed, {
// foreignKey: 'idShed'
// });
};
return model;
};[]
equipment:
import { Sequelize, DataTypes } from 'sequelize';
export default (sequelize, dataTypes) => {
const model = sequelize.define('equipment', {
id: {
type: dataTypes.STRING(200),
primaryKey: true,
allowNull: false,
unique: true,
required: true
},
description: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
idArea: {
type: dataTypes.STRING(200),
allowNull: true,
required: false
},
idHangar: {
type: dataTypes.STRING(200),
allowNull: true,
required: false
},
idControlPlan: {
type: dataTypes.STRING(200),
allowNull: true,
required: false
},
dateControlPlan: {
type: dataTypes.DATE,
allowNull: true,
required: false
},
idUserControlPlan: {
type: dataTypes.STRING(200),
allowNull: true,
required: false
},
status: {
type: dataTypes.BOOLEAN,
allowNull: true,
defaultValue: true
}
}).schema('public');
model.associate = (models) => {
model.hasOne(models.local, {
foreignKey: 'idEquipment'
});
};
return model;
};
shed:
import { Sequelize, DataTypes } from 'sequelize';
export default (sequelize, dataTypes) => {
const model = sequelize.define('shed', {
id: {
type: dataTypes.STRING(200),
primaryKey: true,
allowNull: false,
unique: true,
required: true
},
idArea: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
description: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
status: {
type: dataTypes.BOOLEAN,
allowNull: true,
defaultValue: true
}
}).schema('public');
model.associate = (models) => {
// model.hasOne(models.local, {
// foreignKey: 'idShed'
// });
model.belongsTo(models.area, {
foreignKey: 'idArea'
});
};
return model;
};
area:
import { Sequelize, DataTypes } from 'sequelize';
export default (sequelize, dataTypes) => {
const model = sequelize.define('area', {
id: {
type: dataTypes.STRING(200),
primaryKey: true,
allowNull: false,
unique: true,
required: true
},
description: {
type: dataTypes.STRING,
unique: true,
allowNull: false,
required: true
},
status: {
type: dataTypes.BOOLEAN,
allowNull: true,
defaultValue: true
}
}).schema('public');
model.associate = (models) => {
model.belongsToMany(models.company, {
through: 'companyArea',
foreignKeyConstraint: true,
foreignKey: 'idArea'
});
model.hasOne(models.shed, {
foreignKey: 'idArea'
});
model.hasOne(models.local, {
foreignKey: 'idArea'
});
};
return model;
};
When I add the shed relationship, it informs me that there is no relationship, when shooting everything will normally:
[SQL Error] SequelizeDatabaseError: relation "public.sheds" does not exist EXIT
[SQL Error] relation "public.sheds" does not exist EXIT
I am using postgres database.
Where could that be the mistake, would it be a writing error? Or a template can not have a belongsTo and hasOne relationship at the same time?
Good day. I have 3 tables:
People: id, name
Places: id, name
PeoplePlaces: id, person_id, place_id
And i have models for this tables:
function Person(Sequelize, sequelize) {
return sequelize.define('people', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
firstName: {
type: Sequelize.STRING,
allowNull: false
},
lastName: {
type: Sequelize.STRING,
allowNull: false
},
birthdate: {
type: Sequelize.DATE,
allowNull: false
}
}, {
timestamps: false
});
}
function Place(Sequelize, sequelize) {
return sequelize.define('places', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: Sequelize.STRING,
allowNull: false
}
}, {
timestamps: false
});
}
function PersonPlace(Sequelize, sequelize) {
return sequelize.define('peopleplaces', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
}
}, {
timestamps: false
});
}
So, e.g. i want to delete person with id = 1:
sequelize.people.destroy({ where: { personId: pId } })
But i have an error on deleting, that says to me that table PersonPlaces contains FK to Person and it cannot be deleted.
I try to use onDelete: 'cascade': People.belongsToMany(Places, { through: PeoplePlaces, onDelete: 'cascade' });
But without good result.
So, how i can delete one person from People that have FK on it from PeoplePlaces?