hasMany: destroyAssociations
See original GitHub issueHello,
I’m trying to figure out if there is a way to remove associations from table instead of setting foreign key to null when i call setAssociations(null)
.
For example, if i have Author and Post models:
const Author = Sequelize.define('author', {
name: Sequelize.STRING
});
const Post = Sequelize.define('post', {
title: Sequelize.STRING
});
Author.hasMany(Post);
Post.belongsTo(Author);
And i want to remove all posts for this author, I would like to use something like this:
const author = Author.findById(1);
author.setPosts([]).then(() => ...);
But the problem is that this just sets authorId
to NULL in posts
table. How can i literally remove it from the table?
Thanks.
Issue Analytics
- State:
- Created 7 years ago
- Comments:17 (6 by maintainers)
Top Results From Across the Web
Destroy associations after the last has_many :through record ...
The solution I have found seems to be an after_destroy callback, such as this: class Parent < ActiveRecord::Base has_many :children, :through => :parentage ......
Read more >[Associations] HasMany Method remove[Associations] use ...
Hi, I have a problem with this. I have one to many models, and want to remove the associations and really delete from...
Read more >Active Record Associations - Ruby on Rails Guides
Automatic deletion of join models is direct, no destroy callbacks are triggered. The has_many :through association is also useful for setting up "shortcuts" ......
Read more >Destroying Dependent Models (How To) - Treehouse
Destroying Dependent Models ... If you destroy a record that has_many associated records, by default, those associated records will be left in the...
Read more >Rails for Beginners Part 39: Dependent Destroy Model ...
Rails for Beginners Part 39: Dependent Destroy Model Associations. 2.5K views 1 year ago. GoRails. GoRails. 21.6K subscribers. Subscribe.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
master 😃
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂