antonioribeiro / tracker

Laravel Stats Tracker
MIT License
2.89k stars 594 forks source link

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

Open ndberg opened 7 years ago

ndberg commented 7 years ago

I got this error:

Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_agent' in 'field list' (SQL: updatetracker_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 commented 6 years ago

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 commented 6 years ago

+1 I also have the same problem..

maikobritzke commented 6 years ago

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 commented 6 years ago

I also have same problem. :D

floodedcodeboy commented 6 years ago

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']);
        });
    }
}
emmerink commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

semasping commented 6 years ago

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

floodedcodeboy commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

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

floodedcodeboy commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

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.

mintalicious commented 6 years ago

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 commented 5 years ago

@mintalicious same issue, Any good solution yet?

mintalicious commented 5 years ago

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

gabriel-cardoso commented 5 years ago

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 commented 5 years ago

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.

moheb1 commented 5 years ago

I also have same problem...

drbyte commented 5 years ago

Me too. Laravel 5.8. Googlebot triggered it.

ionesculiviucristian commented 5 years ago
            // 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 commented 5 years ago

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 commented 5 years ago

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.