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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。