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!

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

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R