Skip to content

Querying Sql Model relationship existence with mongo model #1035

@haridarshan

Description

@haridarshan

Hi,

Getting this error when am querying sql model with mongo model relation existence

Fatal error:  Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'exists ?) order by `modified_at` desc' at line 1 in /var/www/test/vendor/illuminate/database/Connection.php:333
Stack trace:
#0 /var/www/test/vendor/illuminate/database/Connection.php(333): PDO->prepare('select * from `...')
#1 /var/www/test/vendor/illuminate/database/Connection.php(722): Illuminate\Database\Connection->Illuminate\Database\{closure}(Object(Illuminate\Database\MySqlConnection), 'select * from `...', Array)
#2 /var/www/test/vendor/illuminate/database/Connection.php(685): Illuminate\Database\Connection->runQueryCallback('select * from `...', Array, Object(Closure))
#3 /var/www/test/vendor/illuminate/database/Connection.php(349): Illuminate\Database\Connection->run('select * from `...', Array, Object(Closure))
#4 /var/www/test/vendor/illuminate/database/Query/Builder.php(161 in
/var/www/test/vendor/illuminate/database/Connection.php on line 729 

Sql Model

namespace App\Models\Sql;

use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\HybridRelations;

class History extends Model
{
	use HybridRelations;
	
	/** @var string Connection Name */
	protected $connection = 'default';
	
	/**
	 * History with Campagin Details
	 * @return \Jenssegers\Mongodb\Relations\HasOne
	 */
	public function campaign()
	{
		return $this->hasOne('\App\Models\Mongo\Campaign', 'campaign.campaign_id', 'campaign_id');
	}
}

This way am checking relation existence. If i remove has it works fine but using has throws the PDOException error

$history = History::where([
	// some condition
])
->has('campaign')
->with('campaign')
->orderBy('modified_at', 'desc')
->get();

and here is the sql query formed

select * from `user_campaign_history` where ( <some_conditions> = ?) and exists (select * from `campaigns` where `campaign`.`campaign_id` exists ?) order by `modified_at` desc

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions