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

Queued job fails when using subqueries with database driver #7

Closed
kenwilliams5 opened this issue Dec 19, 2018 · 5 comments
Closed

Queued job fails when using subqueries with database driver #7

kenwilliams5 opened this issue Dec 19, 2018 · 5 comments

Comments

@kenwilliams5
Copy link

After introducing the example "last login" to my project, a queued job fails when the database driver is being used. It works when using the sync driver.

Snippet from my user model:

protected static function boot()
    {
        parent::boot();

        static::addGlobalScope('lastLogin', function ($query) {
            $query->withLastLoginLink();
        });
    }

public function scopeWithLastLoginLink($query)
    {
        $query->addSubSelect('last_login_id', Login::select('id')
            ->whereColumn('user_id', 'users.id')
            ->latest()
        )->with('lastLoginLink');
    }

public function lastLoginLink()
    {
        return $this->belongsTo(Login::class);
    }

My function that dispatches the job:

protected function notifyOfNewComment($comment)
    {
        $users = $this->subscriptions
            ->where('user_id', '!=', $comment->user_id)->pluck('user_id');
        foreach ($users as $userid) {
            dispatch(new SendNewCommentEmail($this, $userid, $comment));
        }
        if ($comment->rec_can_see) {
            dispatch(new SendNewCommentEmail($this, $this->owner->id, $comment));
        }
    }

The job:

class SendNewCommentEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $gift;
    protected $user;
    protected $comment;

    public $tries = 5;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($gift, $user, $comment)
    {
        $this->gift = $gift;
        $this->user = User::getUser($user);
        $this->comment = $comment;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Mailer $mailer)
    {
        if ($this->user->id != $this->gift->user_id) {
            $mailer->to($this->user->email)->send(new NewComment($this->gift, $this->comment));
        } else {
            $mailer->to($this->user->email)->send(new MyNewComment($this->gift, $this->comment));
        }
    }
}

And the error I get:

Exception message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'integer_in_raw' in 'where clause' (SQL: select * from users where integer_in_raw = users.id)

Job class: App\Jobs\SendNewCommentEmail

Job body: {"displayName":"App\Jobs\SendNewCommentEmail","job":"Illuminate\Queue\CallQueuedHandler@call","maxTries":5,"timeout":null,"timeoutAt":null,"data":{"commandName":"App\Jobs\SendNewCommentEmail","command":"O:28:\"App\Jobs\SendNewCommentEmail\":11:{s:7:\"\u0000\u0000gift\";O:45:\"Illuminate\Contracts\Database\ModelIdentifier\":4:{s:5:\"class\";s:8:\"App\Gift\";s:2:\"id\";i:22;s:9:\"relations\";a:3:{i:0;s:5:\"owner\";i:1;s:19:\"owner.lastLoginLink\";i:2;s:13:\"subscriptions\";}s:10:\"connection\";s:5:\"mysql\";}s:7:\"\u0000\u0000user\";O:45:\"Illuminate\Contracts\Database\ModelIdentifier\":4:{s:5:\"class\";s:8:\"App\User\";s:2:\"id\";i:1;s:9:\"relations\";a:0:{}s:10:\"connection\";s:5:\"mysql\";}s:10:\"\u0000\u0000comment\";O:45:\"Illuminate\Contracts\Database\ModelIdentifier\":4:{s:5:\"class\";s:11:\"App\Comment\";s:2:\"id\";i:457;s:9:\"relations\";a:1:{i:0;s:4:\"gift\";}s:10:\"connection\";s:5:\"mysql\";}s:5:\"tries\";i:5;s:6:\"\u0000\u0000job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:5:\"delay\";N;s:7:\"chained\";a:0:{}}"}}

Exception: 
#0 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php(624): Illuminate\Database\Connection->runQueryCallback('select from `...', Array, Object(Closure)) 
#1 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php(333): Illuminate\Database\Connection->run('select from ...', Array, Object(Closure)) 
#2 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2027): Illuminate\Database\Connection->select('select * from...', Array, true) 
#3 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2015): Illuminate\Database\Query\Builder->runSelect() 
#4 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2499): Illuminate\Database\Query\Builder->Illuminate\Database\Query{closure}() 
#5 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2016): Illuminate\Database\Query\Builder->onceWithColumns(Array, Object(Closure)) 
#6 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(516): Illuminate\Database\Query\Builder->get(Array) 
#7 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(500): Illuminate\Database\Eloquent\Builder->getModels(Array) 
#8 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php(155): Illuminate\Database\Eloquent\Builder->get(Array) 
#9 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php(144): Illuminate\Database\Eloquent\Relations\Relation->get() 
#10 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(564): Illuminate\Database\Eloquent\Relations\Relation->getEager() 
#11 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(533): Illuminate\Database\Eloquent\Builder->eagerLoadRelation(Array, 'owner', Object(Closure)) 
#12 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(490): Illuminate\Database\Eloquent\Builder->eagerLoadRelations(Array) 
#13 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php(85): Illuminate\Database\Eloquent\Model->load(Array) 
#14 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php(55): App\Jobs\SendNewCommentEmail->restoreModel(Object(Illuminate\Contracts\Database\ModelIdentifier)) 
#15 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Queue/SerializesModels.php(45): App\Jobs\SendNewCommentEmail->getRestoredPropertyValue(Object(Illuminate\Contracts\Database\ModelIdentifier)) 
#16 [internal function]: App\Jobs\SendNewCommentEmail->__wakeup() 
#17 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(42): unserialize('O:28:"App\Jobs\...') 
#18 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(83): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\DatabaseJob), Array) 
#19 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(327): Illuminate\Queue\Jobs\Job->fire() 
#20 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(277): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Queue\WorkerOptions)) 
#21 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(118): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\DatabaseJob), 'database', Object(Illuminate\Queue\WorkerOptions)) 
#22 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(102): Illuminate\Queue\Worker->daemon('database', 'default', Object(Illuminate\Queue\WorkerOptions)) 
#23 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(86): Illuminate\Queue\Console\WorkCommand->runWorker('database', 'default') 
#24 [internal function]: Illuminate\Queue\Console\WorkCommand->handle() 
#25 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array) 
#26 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container{closure}() 
#27 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure)) 
#28 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Container/Container.php(572): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL) 
#29 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\Container\Container->call(Array) 
#30 /var/www/html/dev.test/public_html/vendor/symfony/console/Command/Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle)) 
#31 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle)) 
#32 /var/www/html/dev.test/public_html/vendor/symfony/console/Application.php(886): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) 
#33 /var/www/html/dev.test/public_html/vendor/symfony/console/Application.php(262): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) 
#34 /var/www/html/dev.test/public_html/vendor/symfony/console/Application.php(145): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) 
#35 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Console/Application.php(89): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) 
#36 /var/www/html/dev.test/public_html/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(122): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) 
#37 /var/www/html/dev.test/public_html/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) 
#38 {main}

I cannot figure out where the 'integer_in_raw' column it is complaining about is coming from. It doesn't have the problem when the queue driver is "sync"!

@reinink
Copy link
Owner

reinink commented Dec 20, 2018

Sorry @kenwilliams5, I've had a good look at this, but I honestly have no idea where this "integer_in_raw" column is coming from. Have you tried searching your entire project for it?

@kenwilliams5
Copy link
Author

Yes, I have searched for that string from the root directory down...not just the app directory, but everywhere! It only shows up in the log files!

@kenwilliams5
Copy link
Author

BTW...this was a currently running production project, so not all users have login data yet. I don't know if that makes a difference, but thought I'd mention it.

@reinink
Copy link
Owner

reinink commented Dec 20, 2018

I'd recommend working it back, and figuring out which query is running to cause this, and then analyzing how that query is being built. Use the $query->toSql() method to help debug this. This is REALLY strange, but there has to be a logical explanation for it. None of the code you've posted to this point explains it.

@reinink
Copy link
Owner

reinink commented Dec 22, 2018

Closing this for now. Please post back here if you figure this out!

@reinink reinink closed this as completed Dec 22, 2018
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

2 participants