mongify icon indicating copy to clipboard operation
mongify copied to clipboard

Can mongify do `has and belongs to many`?

Open etodanik opened this issue 10 years ago • 10 comments

I'm trying to migrate a database that among other things has a has and belongs to many relationship.

Basically, I have a users table, a roles table, and a roles_users table that has user_id and role_id.

Can I easily embed role into user?

etodanik avatar Nov 19 '14 16:11 etodanik

Unfortunately it does not. I'm looking at supporting it in a future version of Mongify.

anlek avatar Nov 24 '14 05:11 anlek

Is there anything I can do to help? Would you accept a Pull Request? If so - give me kind of a 101 on your project structure, and where you'd be implementing a HABT support structure.

etodanik avatar Feb 20 '15 12:02 etodanik

Yes, I would gladly accept pull requests. As for a 101 of project structure, it's pretty straight forward, if you start looking at https://github.com/anlek/mongify/blob/master/lib/mongify/translation/process.rb and processor_common.rb. That's where all the magic happens ;)

anlek avatar Feb 20 '15 21:02 anlek

@anlek is this feature request still open?

DaanGeurts avatar Aug 06 '16 16:08 DaanGeurts

Yes, unfortunately I haven't had much time to work on Mongify (outside of small updates).

anlek avatar Aug 09 '16 14:08 anlek

Thanks for your work so far! Maybe i have some time to start the implementation.

DaanGeurts avatar Aug 09 '16 18:08 DaanGeurts

Hi @anlek et al,

Does anyone have a suggested work-around for many-to-many relationships with mongify?

Using the example above, and assuming users and roles should become separate documents in mongo, you would need to create both to get the mongo id.

In other words, is there a way I can add roles to the user object like so:

user: {
    roles: [
        {role_id: 'aaaaa...aaa01'},
        {role_id: 'aaaaa...aaa02'}
    ]
}

rot26 avatar Sep 20 '16 23:09 rot26

@rot26, I don't think you can do this with the way Mongify works. There would have to be a after_save to make this work. I'm currently swamped with work but I am hoping to re-write mongify so that it will allow you more flexibility but at the moment, it's just not possible.

You can try playing around with before save and see if you can figure out a way to copy the old DB id into role_id and somehow update it afterwards. However, this is outside of the scope of help I can provide here.

anlek avatar Sep 26 '16 19:09 anlek

any updates on many-to-many relationships with mongify?

MURL1DHAR avatar Jan 26 '17 00:01 MURL1DHAR

Sorry, I've been swamped and have no time to dedicate to this. But will gladly review PR from anyone.

Take care, Andrew

anlek avatar Jan 26 '17 01:01 anlek