Skip to content

Exception: BelongsToMany Is Not supported for hybrid query constraints! #1293

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

Closed
benargo opened this issue Sep 7, 2017 · 3 comments
Closed

Comments

@benargo
Copy link
Contributor

benargo commented Sep 7, 2017

My application is throwing the following exception on my many-to-many (BelongsToMany) relationships, even though I'm not using hybrid relations.

This appears to have cropped up ever since #1195 was merged. I have been running using 'dev-master' for testing. Rolling back to the latest stable release (v3.2.2) fixes the issue (although the issue fixed by #1158 still hasn't been tagged into the stable release).

Here's the exception and stack trace:

[2017-09-07 12:56:36] local.ERROR: Exception: BelongsToMany Is Not supported for hybrid query constraints! in /Users/b-argo/Dropbox/Projects/majic-lamp/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php:115
Stack trace:
#0 /Users/b-argo/Dropbox/Projects/majic-lamp/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php(96): Jenssegers\Mongodb\Eloquent\Builder->getRelatedConstraintKey(Object(Jenssegers\Mongodb\Relations\BelongsToMany))
#1 /Users/b-argo/Dropbox/Projects/majic-lamp/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php(33): Jenssegers\Mongodb\Eloquent\Builder->addHybridHas(Object(Jenssegers\Mongodb\Relations\BelongsToMany), '>=', 1, 'and', Object(Closure))
#2 /Users/b-argo/Dropbox/Projects/majic-lamp/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php(121): Jenssegers\Mongodb\Eloquent\Builder->has(Object(Jenssegers\Mongodb\Relations\BelongsToMany), '>=', 1, 'and', Object(Closure))
#3 /Users/b-argo/Dropbox/Projects/majic-lamp/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1329): Illuminate\Database\Eloquent\Builder->whereHas('venues', Object(Closure))
#4 /Users/b-argo/Dropbox/Projects/majic-lamp/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Eloquent/Model.php(420): Illuminate\Database\Eloquent\Model->__call('whereHas', Array)
#5 /Users/b-argo/Dropbox/Projects/majic-lamp/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1341): Jenssegers\Mongodb\Eloquent\Model->__call('whereHas', Array)
#6 /Users/b-argo/Dropbox/Projects/majic-lamp/app/Http/Controllers/Directory/CategoriesController.php(45): Illuminate\Database\Eloquent\Model::__callStatic('whereHas', Array)
#7 [internal function]: Genii\Http\Controllers\Directory\CategoriesController->getDrinkCategories(Object(Illuminate\Http\Request))

This is the code block which is causing the exception:

$categories = Drink::whereHas('venues', function ($query) use ($location) {
    $query->where('address.geocode', 'near', [
        '$geometry' => ['type' => 'Point', 'coordinates' => [
            $location['longitude'],
            $location['latitude'],
        ]],
        '$maxDistance' => config('location.radius')
    ]);
})->get(['name']);

Having done a bit more digging, it appears to be passing the ($this->getModel() instanceof Model) conditional, found on line 30 of Helpers\QueriesRelations.php and therefore treating it as a hybrid relationship. I have been able to temporarily skirt round the issue by adding this to Helpers\QueriesRelations@getRelatedConstraintKey.

benargo added a commit to benargo/laravel-mongodb that referenced this issue Sep 7, 2017
This temporarily skirts around the issue described in the following issue: mongodb#1293
@novice-dev
Copy link

Hello guys,
I had the same issue, for others still working with laravel 5.4, like me
I could overcome this issue, by checking out to this commit
you can reference it from composer with:
"jenssegers/mongodb": "dev-master#c4a5264be7b9164eb728614f1d6dc15767e5ae60"
Hope this would temporarily solve your problem.

@benargo
Copy link
Contributor Author

benargo commented Oct 9, 2017

I upgraded to v3.2.3 earlier today, and this issue is still present.

The workaround I mentioned still works, so I'm going to submit a pull request to get it incorporated. :)

benargo added a commit to benargo/laravel-mongodb that referenced this issue Apr 10, 2018
This temporarily skirts around the issue described in the following issue: mongodb#1293
D-02022020 pushed a commit to hoffnungslabor/laravel-mongodb that referenced this issue Apr 20, 2018
This temporarily skirts around the issue described in the following issue: mongodb#1293

(cherry picked from commit ebf5a38)
D-02022020 pushed a commit to hoffnungslabor/laravel-mongodb that referenced this issue Apr 20, 2018
Fix mongodb#1293: Exception: BelongsToMany Is Not supported for hybrid query constraints! mongodb#1324
@inquisitive-stha
Copy link

I am facing the same error in 2019. Any solution?

My models

##User

    public function emailAccounts()
    {
        return $this->belongsToMany(EmailAccount::class,null,'user_ids','email_account_ids');
    }

##EmailAccount

    public function users()
    {
        return $this->belongsToMany(User::class,null,'email_account_ids','user_ids');
    }

Error

    "message": "BelongsToMany is not supported for hybrid query constraints.",
    "exception": "Exception",
    "file": "D:\\wamp64\\www\\mongodb-project\\vendor\\jenssegers\\mongodb\\src\\Jenssegers\\Mongodb\\Helpers\\QueriesRelationships.php",
    "line": 168,

softdevee added a commit to softdevee/laravel-mongodb that referenced this issue Sep 5, 2022
This temporarily skirts around the issue described in the following issue: mongodb/laravel-mongodb#1293
lisadeloach63 added a commit to lisadeloach63/mongodb-laravel that referenced this issue Oct 7, 2022
This temporarily skirts around the issue described in the following issue: mongodb/laravel-mongodb#1293
PermitinYury pushed a commit to PermitinYury/laravel-mongodb that referenced this issue Feb 17, 2023
This temporarily skirts around the issue described in the following issue: mongodb/laravel-mongodb#1293
KarenEtheridg pushed a commit to KarenEtheridg/laravel-mongodb that referenced this issue Feb 17, 2023
This temporarily skirts around the issue described in the following issue: mongodb/laravel-mongodb#1293
mnphpexpert added a commit to mnphpexpert/laravel-mongodb that referenced this issue Sep 2, 2024
This temporarily skirts around the issue described in the following issue: mongodb#1293
mnphpexpert added a commit to mnphpexpert/laravel-mongodb that referenced this issue Sep 2, 2024
 Fix mongodb#1293: Exception: BelongsToMany Is Not supported for hybrid query constraints! mongodb#1324
Giant775 added a commit to Giant775/laravel_MongoDB that referenced this issue Nov 15, 2024
This temporarily skirts around the issue described in the following issue: mongodb/laravel-mongodb#1293
dev-arrow added a commit to dev-arrow/laravel-mongodb that referenced this issue Nov 26, 2024
This temporarily skirts around the issue described in the following issue: mongodb/laravel-mongodb#1293
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

3 participants