Repository navigation
Conversation
Codecov Report
@@ Coverage Diff @@
## master #10281 +/- ##
===========================================
- Coverage 96.27% 74.77% -21.51%
===========================================
Files 68 85 +17
Lines 9777 8282 -1495
===========================================
- Hits 9413 6193 -3220
- Misses 364 2089 +1725
Continue to review full report at Codecov.
|
|
|
||
| if (options.include) { | ||
| // To support cross database joins on mysql, prefix the database name of the model in case the model is for a different database | ||
| if (this.dialect === 'mysql') { |
There was a problem hiding this comment.
This is a rather crude approach, how will this API work? Can you show an example of include that you can use to cross join with current proposed changes?
There was a problem hiding this comment.
I agree it's a bit crude and seems hacky but I don't see another way of doing this reliably without affecting a lot of the apis and breaking existing features.
In an app I'm working on that uses multiple databases, I have multiple sequelize instances like below:
const Sequelize = require('Sequelize');
const userDb = new Sequelize(/* ... */);
const contentDb = new Sequelize(/* ... */);
// more databases here....
Then, I have several models for each database that are related to each other such as userDb.ClientModel, contentDb.PostModel where each client can have multiple posts so the relationship is defined as below:
userDb.ClientModel.hasMany(contentDb.PostModel, {foreign_key: 'client_id', sourceKey: 'id', constraints: false});
contentDb.PostModel.belongsTo(userDb.ClientModel, {foreign_key: 'client_id`, targetKey: 'id', constraints: false});
Now if I want to query all posts belonging to a user with
userDb.ClientModel.findAll({
include: { model: contentDb.PostModel },
limit: 5
});
it will NOT work since the query will try to look for the PostModel table in the userDb database. However, I made it work with my implementation of prefixing the table name with the db name.
It only becomes an issue when a query includes joins or subqueries however, I'm not super confident if my fix will work for subqueries that run on another table but from my tests. it does work for join queries such as the above explained scenario.
All the above code are somewhat pseudo code and not exactly as the actual code from my project since I'm not at liberty of sharing those code. So if something doesn't add up or not clear enough, feel free to let me know.
|
Any updates on merging this? Would love to help out removing any blocker if there are any? |
|
Just FYI, I have been using this in a production app for the last 3 months and haven't found any issues caused by this addition. @sushantdhiman |
|
Hi @foysalit! Thanks for taking the time for this. It's unfortunate that so much time has passed without a response for you. In my opinion, a documentation change is important for this PR. As @sushantdhiman said:
I think a MCVE showing the usage would be great (not only as a comment, but as real sequelize docs). |
|
|
||
| let joinTableName = this.quoteTable(tableRight, asRight); | ||
|
|
||
| // For mysql, embe |
There was a problem hiding this comment.
Sorry that's probably just a type i left in instead of finishing the comment. Can't recall what i wanted to write there after so long.
There was a problem hiding this comment.
I see. Please just remove the comment then in order not to confuse others 👍
|
Another thing - how does this relate to MariaDB? Since MariaDB and MySQL are closely related, perhaps this could/should be implemented for MariaDB as well? (Sorry if this question does not make sense, I am not very familiar with mysql/mariadb) |
I think my implementation works on both? The app where I am using this connects to a mariadb instance and it's working fine so far. |
Ok I can add documentation but for the MCVE, do you want me to create a repo showing how it works? or just describing it through documentation is good enough? |
Ah! Great. Sorry for the confusion. By seeing only |
Just including self-contained code and instructions in the documentation is enough 👍 |
|
When will this be merged? And what would be a workaround until then? |
|
@Phyziik - We can't predict foysalit's availability, so we don't know when this will be merged. But if you are in a big hurry, this is what you could do at your own risk:
|
Thanks for such a quick reply. I agree that this could work, but aside from foysalit's implementation, is there no other way to do joins, using Sequelize, even if its with queries? I tried playing with queries, but the issue comes with targeting the second database. It seems like the query is always made in the scope of the first database, omitting that the second database is there and synced. I'm fairly new with Sequelize, so I could be understanding this all wrong. |
|
@Phyziik Well, I think you could probably make it work using raw queries? You can make a raw query that replicates what the work of foysalit would do automatically. Would this work for you? |
|
Hey @papb sorry for the late response but could you point me to an example that i can replicate for adding example/instruction? should it go somewhere in the |
|
@foysalit Hmm... Don't worry about the docs itself for now. Please fix this and post a MCVE. And by MCVE in this case I just mean improving your code here making it a single code block that I can copy-paste and run (assuming I have setup a local mysql runner correctly). |
|
Also please rebase your branch to fix the conflict in select.test.js that github is pointing out. Let me know if you have trouble rebasing. |
|
Hi all, This could be a great development however I'm not sure if it fits within the Sequelize philosophy. For example, in mySQL you can query tables from different databases using the same connection (with the right permissions):
As far as I understand Sequelize needs an instance per database... lets see if this can work without breaking anything. Anyway thanks! |
|
Hey @papb so I created a gist for MCVE : https://gist.github.com/foysalit/a3176c3d588b27606a0d3a064fd56970 this should help you replicate and confirm that the fix works. |
|
Is there anything else I can provide to get this PR considered? Appreciate any help with the rebase as well. |
|
Hi @foysalit thanks for the heads up! I will change it from |
|
Just passing by to say I didn't forget about this... Please hold on |
Any news about this interesting feature? |
|
Hey @papb hope you're having a great 2020 so far. any updates on this? It's now one of the oldest PRs in the queue. Would love to get the ball rolling towards a merge. |
|
With my last commit, cross db joins now work for has many through relationships. |
|
Hey there :) First of all: Thanks a bunch for your contribution to Sequelize! Much appreciated! A couple of months ago, we have switched from master to main branch as our primary development branch and hence this PR is now outdated :( If you still think this change is making sense, please consider recreating the PR against ✌️ |
|
Closing this since it is still against |
Pull Request check-list
Please make sure to review and check all of these items:
npm run testornpm run test-DIALECTpass with this change (including linting)?Description of change
This is my first contribution to sequelize and I tried to follow all the guidelines but feel free to let me know if any change is needed.
This PR attempts to fix #2049 which is right now the only barrier between cross database join queries.
I have not added any new tests since the current test setup does not seem to support multi databases and I do not want to (read, too scared to) poke around the entire test env setup at the moment. However, it does adjust the existing tests to make sure the change is accounted for.
Fixes #2049