drc15469 2015-06-23 00:09
浏览 38
已采纳

如何在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?

  • 写回答

1条回答 默认 最新

  • dongqi3533 2015-06-23 05:12
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?