question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

hasMany: destroyAssociations

See original GitHub issue

Hello,

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:open
  • Created 7 years ago
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
felixfbeckercommented, Dec 15, 2016

master 😃

0reactions
stale[bot]commented, Jul 23, 2019

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 🙂

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found