Loopback-datasource-juggler: Custom Table Names on hasAndBelongsToMany Relationships

Created on 18 Aug 2014  路  16Comments  路  Source: loopbackio/loopback-datasource-juggler

I've searched through the docs and various repos, but it's possible that I've just missed this.

Is it possible to specify the name of the junction table on a hasAndBelongsToMany relationship? I have an existing database with the following tables:

  • contacts
  • addresses
  • contacts_addresses (contains foreign keys contact_id and addresses_id)

If I understand things correctly, the default schema that hasAndBelongsToMany would expect for this would be contactsaddresses, but I'd obviously prefer not to have to redo my existing table schema (and all other non-Loopback code that refers to it.

Thanks in advance...

major feature

Most helpful comment

All 16 comments

You can probably use hasMany with through option instead of hasAndBelongsToMany.

You can also customize the through model for hasAndBelongsToMany via the through option.

Ah, I was thinking there was likely a way to do that. I seem to have made it work by making another full model (Contact_Address) that maps to the junction table, has the various properties of the table, and has belongsTo relations to each of the two other objects (Contact and Address).

I guess I was hoping that there was a way to just specify a junction table without needing to craft a full-on model for it, but that's not terrible.

Thanks for the quick responses!

@raymondfeng unless you think we should allow the specification of table names on the HABTM-relation level, I'm closing this

I do believe you should allow that. There is a lot of legacy code that used different conventions.

I agree with @punund too.

I also think that would be way easier then using a hasManyThrough.

I was just playing with building a loopback application that could talk to a legacy database, and customizing table names for models at all levels would be a requirement for that.

So this issue being "closed" means it is fixed or it got ignored?!

I will change this to a feature request. We don't have the bandwidth to work on this, but would you guys like to submit a patch?

Just read all of this... So there is still no way to choose a custom name for junction table with HABTM ? This feature become useless for a lot of people working with legacy DB.. I guess for now I will stick with defining the model for the junction table with "belongsTo"... But this is really not the way I was expecting it to work...

+1

Any news about this feature (planned, milestone, abandoned ...) ?

the hasAndBelongsToMany has no interest to be used if we can't customize the name of the table junction. Please take this request. Junctions tables are not models and adding them to modeles folder have no sense.

Closing this as https://github.com/strongloop/loopback-datasource-juggler/pull/1303 landed. Thank you @waldemarzahn for the contribution.

Was this page helpful?
0 / 5 - 0 ratings