Sequelize: belongsToMany and cascade deleting - javascript

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?

Related

How to create a sequelize composite constraint with a local table column and a column present in the foreign key table

I'm using Sequelize and PostgreSQL to create a node app and get stuck in this problem. I have three models in sequelize which are:
BoxTemplate
BoxTemplateWorkspace
Workspace
The model BoxTemplateWorkspace is a Many to many association that links many BoxTemplate to many Workspace. What I'm trying to do is to create a composite unique constraint inside the BoxTemplate model, interconnecting a column Title in my BoxTemplate with a column WorkspaceCode which is inside my BoxTemplateWorkspace.
I already know how to create a composite unique constraint with normal columns and with FK columns that reside in the same table/model. But I'm not able to get through this.
Bellow my code to get a better understanding
BoxTemplate.js
var boxTemplate = sequelize.define('BoxTemplate', {
ID: {
type: DataTypes.UUIDV4,
defaultValue: sequelize.literal('gen_random_uuid()'),
allowNull: false,
primaryKey: true,
unique: title_WorkspaceCode_unique
},
Title: {
type: DataTypes.STRING(150),
allowNull: false,
unique: true
},
//... more columns
}, {
indexes: [{
unique: 'title_WorkspaceCode_unique',
fields: ['Title', /*here should fit the WorkspaceCode present in BoxTemplateWorkspace*/]
}],
createdAt: 'CreatedDate',
updatedAt: 'LastUpdate',
timestamps: true,
underscored: false,
freezeTableName: true,
});
boxTemplate.associate = models => {
// model that makes a many to many relationship
boxTemplate.hasMany(models.BoxTemplateWorkspace, {foreignKey: 'BoxTemplateCode'});
}
return boxTemplate;
};
BoxTemplateWorkspace
var boxTemplateWorkspace = sequelize.define('BoxTemplateWorkspace', {
ID: {
type: DataTypes.UUIDV4,
defaultValue: sequelize.literal('gen_random_uuid()'),
allowNull: false,
primaryKey: true,
unique: true
},
IsActive: {
type: DataTypes.BOOLEAN,
defaultValue: true,
allowNull: false
},
InactivatedDate: {
type: DataTypes.DATE,
},
},{
timestamps: false,
underscored: false,
freezeTableName: true
});
boxTemplateWorkspace.associate = models => {
boxTemplateWorkspace.belongsTo(models.BoxTemplate, { foreignKey: 'BoxTemplateCode' });
// column that I want to use as a composite unique constraint
boxTemplateWorkspace.belongsTo(models.Workspace, { foreignKey: 'WorkspaceCode' });
boxTemplateWorkspace.belongsTo(models.UserSK, { foreignKey: 'InactivatedByUserCode' });
}
return boxTemplateWorkspace;
};
As suggested here the UserSK and Workspace Models
UserSK.js
var userSk = sequelize.define('UserSK', {
ID: {
type: DataTypes.UUIDV4,
defaultValue: sequelize.literal('gen_random_uuid()'),
allowNull: false,
primaryKey: true,
unique: true
},
Email: {
type: DataTypes.STRING(320),
allowNull: false,
unique: true
},
UserName: {
type: DataTypes.STRING(150),
allowNull: false,
unique: true
},
}, {
createdAt: 'CreatedDate',
updatedAt: 'LastUpdate',
timestamps: true,
underscored: false,
freezeTableName: true
});
userSk.associate = models => {
userSk.hasMany(models.BoxTemplate, {foreignKey: 'CreatedByUserCode'});
userSk.hasMany(models.BoxTemplate, {foreignKey: 'UpdatedByUserCode'});
}
return userSk;
Workspace.js
var workspace = sequelize.define('Workspace', {
ID: {
type: DataTypes.UUIDV4,
defaultValue: sequelize.literal('gen_random_uuid()'),
allowNull: false,
primaryKey: true,
unique: true
},
InvitedDate: {
type: 'TIMESTAMP',
defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
allowNull: true
},
WorkspaceName: {
type: DataTypes.STRING(100),
allowNull: true
}
}, {
timestamps: false,
underscored: false,
freezeTableName: true
});
workspace.associate = models => {
workspace.hasMany(models.BoxTemplateWorkspace, {foreignKey: 'WorkspaceCode'});
}
return workspace;
};

Why is Sequelize only returning one object using findAll()?

My goal is to be able to find all products by their brand name and model name. However, Sequelize is only returning one record out of many other similar records. If it does return more than one record, other records with identical attributes as the first record found will be null. For example, the first record in the array will have the attribute name: iPhone, the second record which has the exact same attribute will be shown as name: null when it should be name: iPhone.
In my database, I have the following tables:
Products, Brands, Models, and Suppliers. The Products table contains foreign keys such as brand_id, model_id, etc.. Brands, Models, and Suppliers have the attribute: id.
I have set the relationship up as the following:
Products.hasOne(Brands, { foreignKey: 'id' });
Products.hasOne(Models, { foreignKey: 'id' });
Products.hasOne(Suppliers, { foreignKey: 'id' });
Brands.belongsTo(Products);
Models.belongsTo(Products);
Suppliers.belongsTo(Products);
In my search function, I attempt to find all products by brand and model name that match my query.
const getSearch = (req, res) => {
const { query: { query } } = req;
Products.findAll({
where: Sequelize.where(Sequelize.fn('concat', Sequelize.col('Brand.name'), ' ', Sequelize.col('Model.name')), {
[Op.substring]: query
}),
include: [
{ model: Brands, attributes: ['name'] },
{ model: Models, attributes: ['name'] },
{ model: Suppliers, attributes: ['name'] },
],
attributes: ['id', 'price']
})
.then((data) => {
console.log(data);
res.send(data);
})
.catch((err) => (console.log(err)));
};
In my database, I have two product rows with the exact same data but different ids. When calling getSearch I expect to see two objects in the array as they have the same brand name and model name. Instead I see one.
Here's what my models look like:
Products
class Products extends Model {
static init(sequelize, DataTypes) {
return super.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
url_id: {
type: DataTypes.INTEGER,
allowNull: true
},
brand_id: {
type: DataTypes.INTEGER,
allowNull: true
},
model_id: {
type: DataTypes.INTEGER,
allowNull: true
},
supplier_id: {
type: DataTypes.INTEGER,
allowNull: true
},
image: {
type: DataTypes.STRING,
allowNull: true
},
description: {
type: DataTypes.STRING,
allowNull: true
},
price: {
type: DataTypes.DOUBLE,
allowNull: true
}
},
{
modelName: 'Products',
timestamps: false,
sequelize
}
);
}
}
Models
class Models extends Model {
static init(sequelize, DataTypes) {
return super.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: true
},
colour_id: {
type: DataTypes.INTEGER,
allowNull: true
},
storage_capacity_id: {
type: DataTypes.INTEGER,
allowNull: true
}
},
{
modelName: 'Models',
timestamps: false,
sequelize
}
);
}
}
Brands
class Brands extends Model {
static init(sequelize, DataTypes) {
return super.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: true
}
},
{
modelName: 'Brands',
timestamps: false,
sequelize
}
);
}
}
Suppliers
class Suppliers extends Model {
static init(sequelize, DataTypes) {
return super.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: true
}
},
{
modelName: 'Suppliers',
timestamps: false,
sequelize
}
);
}
}
What am I doing wrong here?
You have an error in associations. Just change hasOne to hasMany and you are done.

Sequelize - foreign key on create include returns null

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
}

Sequelize assocation to same table

I have a table called documents that has a column called parentId which is a reference to another document record.
With my current code i'm getting the error
insert or update on table "documents" violates foreign key constraint "documents_parentId_fkey"
documents migration
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('documents', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4
},
parentId: {
allowNull: true,
type: Sequelize.UUID,
references: {
model: 'documents',
key: 'id'
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
lastUpdatedAt: {
allowNull: false,
type: Sequelize.DATE
},
lastUpdatedBy: {
allowNull: false,
type: Sequelize.UUID
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('documents')
}
}
document model
'use strict'
module.exports = (sequelize, DataTypes) => {
const document = sequelize.define('document', {
id: {
allowNull: false,
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4
},
parentId: {
allowNull: true,
type: DataTypes.UUID,
references: {
model: 'documents',
key: 'id'
}
},
lastUpdatedBy: {
allowNull: false,
type: DataTypes.UUID
}
},
{
updatedAt: 'lastUpdatedAt'
})
document.associate = function (models) {
document.belongsTo(models.document, { foreignKey: 'parentId' })
}
return document
}
How do you properly do associations to the same table?
I have a self referencing table configured with the constraints: false setting.
MyModel.belongsTo(MyModel, {
as: 'parentMyModel',
foreignKey: 'parentId',
constraints: false,
});
Looks like the constraint is valid (and a good one). My payload that I was submitting had a parent uuid which didn't actually reference any document with that id.
So my code was right, the data I was submitting was wrong.

HasOne and belongsTo relationships in the same model sequelize

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?

Categories

Resources