Issues with implementing SequelizeJS soft deletion
I'm trying to implement 'soft deletion' in SequelizeJS. So, I've put 'paranoid: true' in my model and 'deletedAt' column in migration. I tried to use the answer from the other question, but it didn't work because of different versions. Also, I'm not sure if I wrote my controllers correctly. There is not that much information online, so I'm not sure how to check if I'm doing it correctly. I'm using Sequelize 5.3.0.
Here, is my model:
'use strict';
module.exports = (sequelize, DataTypes) => {
const Properties = sequelize.define('Properties', {
name: {
allowNull: false,
type: DataTypes.STRING
}
}, {
timestamps: true,
paranoid: true
});
Properties.associate = function(models) {
// associations can be defined here
Properties.hasMany(models.Deals, {
foreignKey: 'id',
onDelete: 'CASCADE'
})
};
return Properties;
};
Here is my migration:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Properties', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.INTEGER,
autoIncrement: true
},
name: {
allowNull: false,
type: Sequelize.STRING
}
deletedAt: {
type: Sequelize.DATE
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Properties');
}
};
I've found this solution from official docs, but it doesn't make sense to me:
User.findAll({
include: [{
model: Tool,
where: { name: { [Op.like]: '%ooth%' } },
paranoid: false // query and loads the soft deleted records
}]
});
My getAllProperties controller:
getAllProperties: (req, res, next) => {
return Properties
.all()
.then(properties => res.status(200).json({ status: 'Retrieved all properties', properties }))
.catch(error => console.log(error));
}
My destroyProperty controller:
destroyProperty: (req, res, next) => {
const { id } = req.params;
return Properties
.findById(id)
.then(property => {
if (!property) {
return res.status(404).send({ message: 'Property not found' })
}
return property
.destroy()
.then(() => res.status(200).json({ status: 'Deleted one property', property }))
.catch(error => console.log(error));
})
}
node.js postgresql express sequelize.js sequelize-cli
add a comment |
I'm trying to implement 'soft deletion' in SequelizeJS. So, I've put 'paranoid: true' in my model and 'deletedAt' column in migration. I tried to use the answer from the other question, but it didn't work because of different versions. Also, I'm not sure if I wrote my controllers correctly. There is not that much information online, so I'm not sure how to check if I'm doing it correctly. I'm using Sequelize 5.3.0.
Here, is my model:
'use strict';
module.exports = (sequelize, DataTypes) => {
const Properties = sequelize.define('Properties', {
name: {
allowNull: false,
type: DataTypes.STRING
}
}, {
timestamps: true,
paranoid: true
});
Properties.associate = function(models) {
// associations can be defined here
Properties.hasMany(models.Deals, {
foreignKey: 'id',
onDelete: 'CASCADE'
})
};
return Properties;
};
Here is my migration:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Properties', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.INTEGER,
autoIncrement: true
},
name: {
allowNull: false,
type: Sequelize.STRING
}
deletedAt: {
type: Sequelize.DATE
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Properties');
}
};
I've found this solution from official docs, but it doesn't make sense to me:
User.findAll({
include: [{
model: Tool,
where: { name: { [Op.like]: '%ooth%' } },
paranoid: false // query and loads the soft deleted records
}]
});
My getAllProperties controller:
getAllProperties: (req, res, next) => {
return Properties
.all()
.then(properties => res.status(200).json({ status: 'Retrieved all properties', properties }))
.catch(error => console.log(error));
}
My destroyProperty controller:
destroyProperty: (req, res, next) => {
const { id } = req.params;
return Properties
.findById(id)
.then(property => {
if (!property) {
return res.status(404).send({ message: 'Property not found' })
}
return property
.destroy()
.then(() => res.status(200).json({ status: 'Deleted one property', property }))
.catch(error => console.log(error));
})
}
node.js postgresql express sequelize.js sequelize-cli
add a comment |
I'm trying to implement 'soft deletion' in SequelizeJS. So, I've put 'paranoid: true' in my model and 'deletedAt' column in migration. I tried to use the answer from the other question, but it didn't work because of different versions. Also, I'm not sure if I wrote my controllers correctly. There is not that much information online, so I'm not sure how to check if I'm doing it correctly. I'm using Sequelize 5.3.0.
Here, is my model:
'use strict';
module.exports = (sequelize, DataTypes) => {
const Properties = sequelize.define('Properties', {
name: {
allowNull: false,
type: DataTypes.STRING
}
}, {
timestamps: true,
paranoid: true
});
Properties.associate = function(models) {
// associations can be defined here
Properties.hasMany(models.Deals, {
foreignKey: 'id',
onDelete: 'CASCADE'
})
};
return Properties;
};
Here is my migration:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Properties', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.INTEGER,
autoIncrement: true
},
name: {
allowNull: false,
type: Sequelize.STRING
}
deletedAt: {
type: Sequelize.DATE
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Properties');
}
};
I've found this solution from official docs, but it doesn't make sense to me:
User.findAll({
include: [{
model: Tool,
where: { name: { [Op.like]: '%ooth%' } },
paranoid: false // query and loads the soft deleted records
}]
});
My getAllProperties controller:
getAllProperties: (req, res, next) => {
return Properties
.all()
.then(properties => res.status(200).json({ status: 'Retrieved all properties', properties }))
.catch(error => console.log(error));
}
My destroyProperty controller:
destroyProperty: (req, res, next) => {
const { id } = req.params;
return Properties
.findById(id)
.then(property => {
if (!property) {
return res.status(404).send({ message: 'Property not found' })
}
return property
.destroy()
.then(() => res.status(200).json({ status: 'Deleted one property', property }))
.catch(error => console.log(error));
})
}
node.js postgresql express sequelize.js sequelize-cli
I'm trying to implement 'soft deletion' in SequelizeJS. So, I've put 'paranoid: true' in my model and 'deletedAt' column in migration. I tried to use the answer from the other question, but it didn't work because of different versions. Also, I'm not sure if I wrote my controllers correctly. There is not that much information online, so I'm not sure how to check if I'm doing it correctly. I'm using Sequelize 5.3.0.
Here, is my model:
'use strict';
module.exports = (sequelize, DataTypes) => {
const Properties = sequelize.define('Properties', {
name: {
allowNull: false,
type: DataTypes.STRING
}
}, {
timestamps: true,
paranoid: true
});
Properties.associate = function(models) {
// associations can be defined here
Properties.hasMany(models.Deals, {
foreignKey: 'id',
onDelete: 'CASCADE'
})
};
return Properties;
};
Here is my migration:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Properties', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.INTEGER,
autoIncrement: true
},
name: {
allowNull: false,
type: Sequelize.STRING
}
deletedAt: {
type: Sequelize.DATE
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Properties');
}
};
I've found this solution from official docs, but it doesn't make sense to me:
User.findAll({
include: [{
model: Tool,
where: { name: { [Op.like]: '%ooth%' } },
paranoid: false // query and loads the soft deleted records
}]
});
My getAllProperties controller:
getAllProperties: (req, res, next) => {
return Properties
.all()
.then(properties => res.status(200).json({ status: 'Retrieved all properties', properties }))
.catch(error => console.log(error));
}
My destroyProperty controller:
destroyProperty: (req, res, next) => {
const { id } = req.params;
return Properties
.findById(id)
.then(property => {
if (!property) {
return res.status(404).send({ message: 'Property not found' })
}
return property
.destroy()
.then(() => res.status(200).json({ status: 'Deleted one property', property }))
.catch(error => console.log(error));
})
}
node.js postgresql express sequelize.js sequelize-cli
node.js postgresql express sequelize.js sequelize-cli
asked Nov 17 '18 at 23:05
in43shin43sh
549
549
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I figured out, that my models and migrations were good, the thing is that I was doing sequelize db:migrate:undo:all
and sequelize db:migrate
, but the db schema stayed the same. So, I did sequelize db:drop
and sequelize db:create
and then it started creating this field.
Also, I changed my getAllProperties controller:
getAllProperties: (req, res, next) => {
return Properties
.findAll({paranoid: false})
.then(properties => res.status(200).json({ status: 'Retrieved all properties', properties }))
.catch(error => console.log(error));
}
After I changed all that, it started working.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53356393%2fissues-with-implementing-sequelizejs-soft-deletion%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I figured out, that my models and migrations were good, the thing is that I was doing sequelize db:migrate:undo:all
and sequelize db:migrate
, but the db schema stayed the same. So, I did sequelize db:drop
and sequelize db:create
and then it started creating this field.
Also, I changed my getAllProperties controller:
getAllProperties: (req, res, next) => {
return Properties
.findAll({paranoid: false})
.then(properties => res.status(200).json({ status: 'Retrieved all properties', properties }))
.catch(error => console.log(error));
}
After I changed all that, it started working.
add a comment |
I figured out, that my models and migrations were good, the thing is that I was doing sequelize db:migrate:undo:all
and sequelize db:migrate
, but the db schema stayed the same. So, I did sequelize db:drop
and sequelize db:create
and then it started creating this field.
Also, I changed my getAllProperties controller:
getAllProperties: (req, res, next) => {
return Properties
.findAll({paranoid: false})
.then(properties => res.status(200).json({ status: 'Retrieved all properties', properties }))
.catch(error => console.log(error));
}
After I changed all that, it started working.
add a comment |
I figured out, that my models and migrations were good, the thing is that I was doing sequelize db:migrate:undo:all
and sequelize db:migrate
, but the db schema stayed the same. So, I did sequelize db:drop
and sequelize db:create
and then it started creating this field.
Also, I changed my getAllProperties controller:
getAllProperties: (req, res, next) => {
return Properties
.findAll({paranoid: false})
.then(properties => res.status(200).json({ status: 'Retrieved all properties', properties }))
.catch(error => console.log(error));
}
After I changed all that, it started working.
I figured out, that my models and migrations were good, the thing is that I was doing sequelize db:migrate:undo:all
and sequelize db:migrate
, but the db schema stayed the same. So, I did sequelize db:drop
and sequelize db:create
and then it started creating this field.
Also, I changed my getAllProperties controller:
getAllProperties: (req, res, next) => {
return Properties
.findAll({paranoid: false})
.then(properties => res.status(200).json({ status: 'Retrieved all properties', properties }))
.catch(error => console.log(error));
}
After I changed all that, it started working.
answered Nov 20 '18 at 23:46
in43shin43sh
549
549
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53356393%2fissues-with-implementing-sequelizejs-soft-deletion%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown