mysql 提示错误 doesn't have a default value.
今天遇到了mysql 提示错误 doesn't have a default value.
-
<h1>CDbException</h1>
-
<p>CDbCommand 无法执行 SQL 语句: SQLSTATE[HY000]: General error: 1364 Field 'sms_percent_amount' doesn't have a default value. The SQL statement executed was: INSERT INTO t_dev (`mail`, `passwd`, `name`,`reg_ip`,`git_id`,`status`) VALUES(:mail, :passwd, :name, :reg_ip,:git_id,:status);. Bound with mail='safFD@jj.dk', passwd='e99a18c428cb38d5f260853678922e03', name='magic007', status=1, reg_ip='2130706433', git_id='2484137' (/Users/magic/Applications/web/cctv/yii/framework/db/CDbCommand.php:358)</p><pre>#0 /Users/magic/Applications/web/cctv/cctv-web/www.cctv.cn/protected/models/User.php(329): CDbCommand->execute(Array)
-
#1 /Users/magic/Applications/web/cctv/cctv-web/www.cctv.cn/protected/models/User.php(37): User->addRow(Array)
-
#2 /Users/magic/Applications/web/cctv/cctv-web/www.cctv.cn/protected/controllers/SiteController.php(398): User->register(Array)
-
#3 /Users/magic/Applications/web/cctv/yii/framework/web/actions/CInlineAction.php(49): SiteController->actionAuthRegister()
-
#4 /Users/magic/Applications/web/cctv/yii/framework/web/CController.php(308): CInlineAction->runWithParams(Array)
-
#5 /Users/magic/Applications/web/cctv/yii/framework/web/filters/CFilterChain.php(133): CController->runAction(Object(CInlineAction))
-
#6 /Users/magic/Applications/web/cctv/yii/framework/web/filters/CFilter.php(40): CFilterChain->run()
-
#7 /Users/magic/Applications/web/cctv/yii/framework/web/CController.php(1145): CFilter->filter(Object(CFilterChain))
-
#8 /Users/magic/Applications/web/cctv/yii/framework/web/filters/CInlineFilter.php(58): CController->filterAccessControl(Object(CFilterChain))
-
#9 /Users/magic/Applications/web/cctv/yii/framework/web/filters/CFilterChain.php(130): CInlineFilter->filter(Object(CFilterChain))
-
#10 /Users/magic/Applications/web/cctv/yii/framework/web/CController.php(291): CFilterChain->run()
-
#11 /Users/magic/Applications/web/cctv/yii/framework/web/CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)
-
#12 /Users/magic/Applications/web/cctv/yii/framework/web/CWebApplication.php(282): CController->run('AuthRegister')
-
#13 /Users/magic/Applications/web/cctv/yii/framework/web/CWebApplication.php(141): CWebApplication->runController('site/AuthRegist...')
-
#14 /Users/magic/Applications/web/cctv/yii/framework/base/CApplication.php(185): CWebApplication->processRequest()
-
#15 /Users/magic/Applications/web/cctv/cctv-web/www.cctv.cn/index.php(24): CApplication->run()
-
#16 {main}</pre>
查看目前mysql使用模式
mysql> select @@sql_mode;
+--------------------------------------------+
| @@sql_mode |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+
修改MySQL 模式
命令行修改 SET sql_mode = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";
如果需要重启后修改,请修改my.conf
sql_mode= NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
关于sql_mode 介绍
1. sql_mode模式
mysql数据库的中有一个环境变量sql_mode,定义了mysql应该支持的sql语法,数据校验等!我们可以通过以下方式查看当前数据库使用的sql_mode:
-
mysql> set @@sql_mode=TRADITIONAL;
-
Query OK, 0 rows affected (0.00 sec)
-
-
mysql> create table test(name varchar(4), pass varchar(4));
-
Query OK, 0 rows affected (0.02 sec)
-
-
mysql> insert into test values('aaaaa','aaaaa'),('bbbb','bbbb');
-
ERROR 1406 (22001): Data too long for column 'name' at row 1
-
-
mysql> show errors;
-
+-------+------+------------------------------------------+
-
| Level | Code | Message |
-
+-------+------+------------------------------------------+
-
| Error | 1406 | Data too long for column 'name' at row 1 |
-
+-------+------+------------------------------------------+
-
1 row in set (0.00 sec)
-
-
mysql> select * from test;
-
Empty set (0.00 sec)
TRADITIONAL模式与STRICT_TRANS_TABLES模式执行的结果,在这种情况下一致。
注意:我们这里设置的sql_mode都是session级别的。另外,可以直接修改my.ini文件,找到sql_mode,然后设置新的模式即可!
例如:
vi /etc/my.cnf
在[mysqld]下面添加如下列:
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
阅读(3008) | 评论(0) | 转发(0) |