Search code examples
phpmysqlexceptionlaravel-4cartalyst-sentry

How to debug insert exception in laravel 4 / cartalyst sentry


I took over a project written in Laravel 4. There are two systems (dev and production) which basically should be doing the same.

When I try to register a user (the package used is cartalyst/sentry) it works on production. On the dev environment it fails as there are some columns defined as not null and there is no statement in the code to set it. So I understand why it fails, what I don't understand is why it works on another machine and what kind of tweak I could make on dev to get it working. Without altering the db-columns (I want to understand the system before making such changes).

Exception:

SQLSTATE[HY000]: General error: 1364 Field 'auto_token' doesn't have a default value 
(SQL: insert into `users` (`first_name`, `last_name`, `display_name`, `email`,  
`password`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?)) (Bindings: array ...

Code to regiser a new user:

        $user = Sentry::register(array(
            'first_name' => Input::get('first_name'),
            'last_name'  => Input::get('last_name'),
            'display_name'=> Input::get('display_name'),
            'email'      => Input::get('email'),
            'password'   => Input::get('password'),
        ));

DB-Schema:

id (unsigned int, not null)
first_name (varchar, not null)
last_name (varchar, not null)
display_name (varchar, not null)
email (varchar, not null)
password (varchar, not null)
auto_token (varchar, not null)
... more columns like auto_token

Prod: MySQL 5.5.42 - PHP 5.4.30 - running on Linux

Dev : MySQL 5.6.21 - PHP 5.4.30 - running on Windows

I know that the query should not run successfully in the first place, the database schema in general is flawed. I would like to learn what causes this and how to solve this issue.

Where I got stuck is why on one System it's working and on the other not. Anybody knows a direction?


Solution

  • Looks like your problem is in MySQL configuration. To avoid these errors you have to disable MySQL Strict Mode.

    Find the my.cnf or my.ini configuration file and remove STRICT_TRANS_TABLES from sql-mode value.

    E.g. before edit:
    sql-mode=”STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION”

    After edit:
    sql-mode=”NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION”

    You have to restart mysql after saving the configuration file and everything will probably work as expected.

    On the other hand, disabling "strict mode" is a bad practice and it's a better idea to edit your migrations and add nullable or default in proper places.

    Good luck!