Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Document Support for has_and_belongs_to_many (HABTM) tables #34

Open
prem-prakash opened this issue Nov 10, 2015 · 7 comments
Open

Document Support for has_and_belongs_to_many (HABTM) tables #34

prem-prakash opened this issue Nov 10, 2015 · 7 comments

Comments

@prem-prakash
Copy link

Is it possible to add support to has_and_belongs_to_many tables that has no model ?

@pjambet
Copy link
Contributor

pjambet commented Nov 30, 2015

Hi,

This is currently not supported (I guess the README could be more explicit about this), as we use model classes to generate the seed file.

I think this would be a great feature, but sadly I can't guarantee that I'll have time to work on this anytime soon.

As usual, I'd be very happy to take a look at a PR of you want to implement this feature :)

@tbloncar
Copy link
Contributor

Dug into this a bit this evening, and it appears that the has_and_belongs_to_many join table does actually get a pair of implicit backing models. Given the following:

class Product
  has_and_belongs_to_many :categories
end

class Category
  has_and_belongs_to_many :products
end

We get two backing HABTM models: Product::HABTM_Categories and Category::HABTM_Products. As far as I can tell (having implemented a test for this on my fork), registering either of these models should do the trick.

SeedMigration.register Category::HABTM_Products

I'd be happy to submit a PR with the test and some documentation for this.

@pboling pboling changed the title Support to has_and_belongs_to_many tables Document Support for has_and_belongs_to_many (HABTM) tables Feb 21, 2023
@pboling pboling self-assigned this Feb 21, 2023
@pboling
Copy link
Owner

pboling commented Feb 22, 2023

@tbloncar still interested in submitting a PR for this?

@jfoo1984
Copy link
Contributor

jfoo1984 commented May 29, 2024

I also found that the implicit model exists (though it's a private constant). However, when I register the model, I'm getting an error because the join table does not have an id column

 HABTM_PathogenListVersions Load (1.3ms)  SELECT `citations_pathogen_list_versions`.* FROM `citations_pathogen_list_versions` ORDER BY id
Mysql2::Error: Unknown column 'id' in 'order clause' excluded from capture: Not configured to send/capture in environment 'development'

I tried to exclude the id field as well, but that did not seem to help. It looks like the model query includes an order by ID clause: https://github.com/pboling/seed_migration/blob/85100250c9f241779cb600f1df4030ca19a6b65c/lib/seed_migration/migrator.rb#L258C31-L258C43

@pboling
Copy link
Owner

pboling commented May 30, 2024

I am happy to review a PR, but I won't have time to work on this for some time.

@jfoo1984
Copy link
Contributor

Yea, I forked the repo and am planning to work on a fix today. I'll link it here and open a PR when ready.

@jfoo1984
Copy link
Contributor

I opened a PR (#80) from a branch in my fork. I had meant to just do that in my fork and then merge it upstream, but let me know if this is fine, or if you'd prefer that I redo the PR.

Also, I have no idea if the original poster still needs this, but this PR would probably address #23 as well

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

5 participants