Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[5.4] Cross-connection has not working #17624

Closed
chancezeus opened this issue Jan 28, 2017 · 7 comments
Closed

[5.4] Cross-connection has not working #17624

chancezeus opened this issue Jan 28, 2017 · 7 comments

Comments

@chancezeus
Copy link

chancezeus commented Jan 28, 2017

  • Laravel Version: 5.4.6
  • PHP Version: 7.1.0
  • Database Driver & Version: MySQL 5.7.16

Description:

I have a similar setup as mentioned in #17515 (two schema's on a single server and a couple of relationships between those). I explicitly specified both connections but as soon as I try a has (and probably whereHas too) query from the model on the first connection to a model on the second connection it fails stating that the second model's table can not be found (using the first connections schema), which is correct but wrong, since the relationship should know to look (since it is explicitly specified) in the second connections schema (probably a simple (pseudocode)

if ($model1->getConnectionName() != $model2->getConnectionName()) {
  $model2->{fullyQualifiedColumnName} =
$model2->getConnection()->getSchema().".".$model2->{fullyQualifiedColumnName};
}

would suffice to fix the issue (assuming both schema's are on the same database server (otherwise it would never work, since it is near impossible to do subqueries across different server connections))

Steps To Reproduce:

  1. Create two schema's (ie schema1 & schema2)
  2. Create two models one in each database (ie model1 (model1 $connection == 'schema1connection') & model2 (model2 $connection == 'schema2connection'))
  3. Create a relationship between those models (ie on model1: $this->hasMany(Model2::class))
  4. Execute Model1::has('model2relation')->get()

Expected results

Since both models have their connections explicitly specified I'd expect to see it reflected in the resulting (sub)select (ie

WHERE [`schema1`.]`model1table`.`model1column` = `schema2`.`model2table`.`model2column`

) with schema1 optional since the query runs on this connection

@chancezeus
Copy link
Author

chancezeus commented Jan 28, 2017

A (cleaned up) error message:

Illuminate\Database\QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'schema2.model2' doesn't exist (SQL: select * from `model1` where exists (select * from `model2` where `model1`.`id` = `model2`.`model1_id`) limit 1)'

@chrislandeza
Copy link

This was already reported here: #4649

@chancezeus
Copy link
Author

The report you reference seems to be about 2 different servers, my report is about 1 server 2 schema's which is a different situation (since cross schema (ie. on the same server) JOINs/SUBSELECTs are possible, JOINs/SUBSELECT(s) on a different server not)...

@MalteJanz
Copy link

Hello,

I have the same problem at the moment. I can get for example cars with motors (i use the with function and can also say wich motors i want) without any problems accross two databases on the same server.

But if i want for example only cars with motors where the motor has a specific power (the whereHas function) it is not working.

The real code is about adminlogs and the user -> player. I want to search for logs where the player->name is like something. (adminlog and user are in one database and player is in another, but on the same server). You can see the code on the attached screenshot. A workaround is that i can do it only with the with function and filter through the collection and remove the null relations. But that is not a solution because i call later other where and orderBy functions + paginate and this collection can be very big in the end.

sorry for my english.

screenshot

@skalero01
Copy link

skalero01 commented Dec 7, 2017

Does this issue continue existing or was it fixed? Regards

@MalteJanz
Copy link

I didn't tested it until now and that project with the problem is not running anymore. Maybe someone else could try it out (with a very basic example).

Regards

@maguilar92
Copy link

maguilar92 commented Feb 7, 2018

I added package to manage this issue:

https://github.com/hoyvoy/laravel-cross-database-subqueries

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants