Fix default related table id name when using schema's #2083
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Introduction
This is a fix for the break in the proper naming-convention that happens when using database schema's in bookshelf.
Motivation
Models use a certain table name. Name convention has it (which bookshelf uses as default) that the primary id of this table is formatted to be "tablename_id". This is good. When describing a database (for instance with Postgres) that uses tables across multiple database schema's however the current implementation breaks. Why? Well to use Schema's effectively in bookshelf we can put it in front of the tablename when defining our model (as also discussed in #458). When we do this we can reference tables properly and everything works well, but the default naming convention for the ids needed to construct the queries breaks. Our previously used "tablename_id" now becomes the non standard "schema.tablename_id" because bookshelf thinks schema. is part of the tablename. We should not have to make the name of the id field be "schema.tablename_id" or specifically enter the id (see alternative solution) for bookshelf to work. Instead bookshelf should in this instance (getting the tables primary id by the default name) filter out the schema if those are used.
How I found the issue
Click to reveal
Proposed solution
The current solution filters out any "schema." when generating a default name for referencing the ID field of a model.
Current PR Issues
No. On the previous PR some issues were already solved and it was stated that some tests were required. Apart from my personal tests with postgres and mysql which have not revealed any issues (this is a quite simple fix) perhaps it is good to have some unit tests. Although I am not sure (how) this is required.
Alternatives considered
The only way to make schema's work with the default ID naming convention currently is to use the full method (and thus specifically force the ID):
return this.belongsTo('organisations'); becomes return this.belongsTo('organisations', organisation_id');