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

Column not found: 1054 Unknown column 'user_agent' in 'field list' ... #325

Open
ndberg opened this issue Sep 25, 2017 · 26 comments
Open

Column not found: 1054 Unknown column 'user_agent' in 'field list' ... #325

ndberg opened this issue Sep 25, 2017 · 26 comments
Assignees
Labels

Comments

@ndberg
Copy link
Contributor

ndberg commented Sep 25, 2017

I got this error:

Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_agent' in 'field list' (SQL: update tracker_sessionssetuser_agent= Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0,updated_at= 2017-09-23 06:40:46 whereid= 301550)

But even on a fresh install I don't have this column in the tracker_sessions table! Is there a migration missing or something like that?

thanks

@maikobritzke
Copy link

Hello,

I'm also having the same problem (laravel + pgsql)

Next Illuminate\Database\QueryException: SQLSTATE[42703]: Undefined column: 7 ERROR: column "user_agent" of relation "tracker_sessions" does not exist LINE 1: update "tracker_sessions" set "user_agent" = $1, "updated_at... ^ (SQL: update "tracker_sessions" set "user_agent" = Mozilla/5.0 (Windows Phone 8.1; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 630 Dual SIM) like Gecko, "updated_at" = 2017-09-27 08:39:09 where "id" = 1036) in /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:647

There is the agent_id column, but not the user_agent column.
Is there any correction for this?

@nurwin
Copy link

nurwin commented Sep 28, 2017

+1
I also have the same problem..

@maikobritzke
Copy link

Hello,

I resolved temporarily:

In the ensureSessionDataIsComplete() function of the file "pragmarx/tracker/src/Data/Repositories/Session.php" +or- in line 135, I added this:

if($key === 'user_agent') continue;

@endytc
Copy link

endytc commented Oct 4, 2017

I also have same problem. :D

@floodedcodeboy
Copy link

floodedcodeboy commented Oct 29, 2017

same issue here. Followed instructions as per readme.

edit:

I created this migration to deal with this issue

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class TrackerUserAgentFieldToTrackerSessions extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
        Schema::connection('tracker')->table('tracker_sessions', function (Blueprint $table) {
            $table->string('user_agent')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::connection('tracker')->table('tracker_sessions', function (Blueprint $table) {
            $table->dropColumn(['user_agent']);
        });
    }
}

simonepozzobon added a commit to simonepozzobon/The-Film-Corner that referenced this issue Nov 6, 2017
@emmerink
Copy link

Got this error as well. Wondering if adding the column would be the right solution as there is already an agent_id in the table. Shouldn't it be stored in the tracker_agents instead?

@atiehmokhtary
Copy link

hello, I am getting this error too but it just appears when I refresh my pages on mobile views.
any one found any reliable solusion? I'm not sure about creating that migration @floodedcodeboy added.

@akaamitgupta
Copy link

@antonioribeiro Faced the similar issue is there any reliable solution? Please assist.

@semasping
Copy link

semasping commented Apr 24, 2018

+1
I also have the same problem..
on dev server - ok
on production server have this error
in config:
'log_user_agents' => false,

@floodedcodeboy
Copy link

How is a migration now a reliable and repeatable solution? :D You'll find that this repo relies on them heavily. @akaamitgupta @atiehmokhtary

Sorry - maybe I'll just create a PR for that migration when I get the chance. Then the work is done for you, if it's not already.

@floodedcodeboy
Copy link

floodedcodeboy commented Apr 24, 2018

@semasping did you run the migration I provided? are your db's in sync? Can you confirm the column is not on production but it is on your dev?

Also if you want to switch off "Tracker" completely you'll have to set more than 'log_user_agents' => false

Try by starting with this one (as per the docs) : 'log_enabled' => false,

@semasping
Copy link

@floodedcodeboy
if set 'log_user_agents' => true
then error is gone

@floodedcodeboy
Copy link

@semasping Thanks, it wasn't clear...
I take it you're not using my flawed migration?
also - see here : https://github.com/antonioribeiro/tracker/blob/master/src/Tracker.php#L118

@semasping
Copy link

@floodedcodeboy
no, migration from this issue didn`t use on any server

the column is not on production and is not on dev servers

@floodedcodeboy
Copy link

@semasping apparently my migration is missing something - however I know that it resolves the issue. Note - it has not been tested extensively.

Sounds like you have a night of debugging ahead of you - sorry i can't help more at this point.

@alexandreMesle
Copy link

I ran into the same error. And it comes out whathever the value of 'log_user_agents'.

I agree with @emmerink : the agents should be stored in the tracker_agents and referenced with the agent_id field.

I tried to create a user_agent field in the tracker_sessions table, as suggested by @floodedcodeboy, and I have a good new : it works ! But don't be fooled, "Working" here means "The error message has disapeared"...

I really think it is no more than a workaround : it gives the developpers the time to correct the bug, but leaving the database this way would be a great misuse of the relationnal databases.

ben-nsng added a commit to ben-nsng/tracker that referenced this issue May 2, 2018
@mintalicious
Copy link

I observed the same behavior in one of my apps while changing from desktop to mobile view in chromium and then reloading the page.
The point is that the key 'user_agent' is built in vendor/pragmarx/tracker/src/Tracker.php in makeSessionData() and then later in vendor/pragmarx/tracker/src/Data/Repositories/Session.php in ensureSessionDataIsComplete() it wants to be stored back as an attribute of tracker_session.

The simplest workaround might be maikos code trick from September 2017 by just ignoring this field in the update loop. But there should also exist a cleaner way. Maybe @antonioribeiro has an idea?

@meeprakash25
Copy link

@mintalicious same issue, Any good solution yet?

@mintalicious
Copy link

Nope, sorry. Still using the workaround mentoined above. KR

@gabriel-cardoso
Copy link
Contributor

In the file vendor/pragmarx/tracker/src/Tracker.php you can see

// The key user_agent is not present in the sessions table, but
// it's internally used to check if the user agent changed
// during a session.
'user_agent' => $this->dataRepositoryManager->getCurrentUserAgent(),

I'm not sure the migration is the best solution.

A hint from @antonioribeiro would be appreciated !

@hktang
Copy link

hktang commented Feb 1, 2019

It's been working great since I started to use it in late 2018. Today I encounter this error in the development environment (Laradock).

The error appears using Firefox 65.0.
There's no error using Chrome.

Hope this information helps!
For now, I have disabled Tracker on both production and development.

@napestershine napestershine self-assigned this Feb 4, 2019
@moheb1
Copy link

moheb1 commented Apr 14, 2019

I also have same problem...

@drbyte
Copy link

drbyte commented Apr 23, 2019

Me too. Laravel 5.8. Googlebot triggered it.

@ionesculiviucristian
Copy link

ionesculiviucristian commented May 14, 2019

            // The key user_agent is not present in the sessions table, but
            // it's internally used to check if the user agent changed
            // during a session.
            'user_agent' => $this->dataRepositoryManager->getCurrentUserAgent(),

If that is the case then:

        foreach ($this->sessionInfo as $key => $value) {
            if ($key == 'user_agent') {
                continue;
            }
            
            if ($sessionData[$key] !== $value) {
                if (!isset($model)) {
                    $model = $this->find($this->sessionInfo['id']);
                }

                $model->setAttribute($key, $value);

                $model->save();

                $wasComplete = false;
            }
        }

or this but will brake some "rules":

        $userAgent = $sessionData['user_agent'];

        if ($userAgent != $this->sessionInfo['user_agent']) {
            $uaParser = new UserAgentParser(app()->make('path.base'));

            $sessionData['agent_id'] = (new \PragmaRX\Tracker\Vendor\Laravel\Models\Agent())->forceFill([
                'name' => $name = $uaParser->originalUserAgent ?: 'Other',
                'browser' => $uaParser->userAgent->family,
                'browser_version' => $uaParser->getUserAgentVersion(),
                'name_hash' => hash('sha256', $name),
            ])->getKey();
        }

        foreach ($this->sessionInfo as $key => $value) {
            if ($key == 'user_agent') {
                continue;
            }

            if ($sessionData[$key] !== $value) {
                if (!isset($model)) {
                    $model = $this->find($this->sessionInfo['id']);
                }

                $model->setAttribute($key, $value);

                $model->save();

                $wasComplete = false;
            }
        }

Hope this gets fixed as there is no way to switch to mobile testing after refresh.

@doncadavona
Copy link
Contributor

doncadavona commented Jun 24, 2019

Still have this issue. So far for me, it only happens on mobile (via Google Chrome's Device Tool) like so:

Screen Shot 2019-06-25 at 2 17 46 AM

When not in a mobile device, there are no problems. @antonioribeiro, maybe the logging code when on mobile device has not been updated according to the new schema.

doncadavona added a commit to doncadavona/tracker that referenced this issue Jun 24, 2019
doncadavona added a commit to doncadavona/tracker that referenced this issue Jun 24, 2019
@doncadavona
Copy link
Contributor

doncadavona commented Jun 24, 2019

I FIXED this by overriding the class that has the issue with composer:

First, copy the original class from the vendor to your app:
$ cp vendor/pragmarx/tracker/src/Data/Repositories/Session.php app/VendorOverrides/PragmaRX/Tracker/Data/Repositories/Session.php

Second, add the following inside the autoload object in composer.json:
"files": ["app/VendorOverrides/PragmaRX/Tracker/Data/Repositories/Session.php"]

Your composer.json's autoload should be like so:

"autoload": {
    "psr-4": {
        "App\\": "app/"
    },
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "files": ["app/VendorOverrides/PragmaRX/Tracker/Data/Repositories/Session.php"]
},

Third, add the following line after line 134 of app/VendorOverrides/PragmaRX/Tracker/Data/Repositories/Session.php:
if ($key === 'user_agent') continue;

The ensureSessionDataIsComplete() function in our Session.php should now be:

private function ensureSessionDataIsComplete()
{
    $sessionData = $this->getSessionData();
    $wasComplete = true;
    foreach ($this->sessionInfo as $key => $value) {
        if ($key === 'user_agent') continue; // <--- THIS IS WHAT IS NEW!!!
        if ($sessionData[$key] !== $value) {
            if (!isset($model)) {
                $model = $this->find($this->sessionInfo['id']);
            }
            $model->setAttribute($key, $value);
            $model->save();
            $wasComplete = false;
        }
    }
    if (!$wasComplete) {
        $this->storeSession();
    }
}

Finally, run
$ composer dump-autoload.

Pull-requests for this like #391 or #448 are not yet merged. Until that, this should fix this issue.

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

No branches or pull requests