Rails 3.2+;MySQL:错误:字段';创建于';不';没有默认值:插入到

Rails 3.2+;MySQL:错误:字段';创建于';不';没有默认值:插入到,mysql,ruby-on-rails,ruby,timestamp,insert-into,Mysql,Ruby On Rails,Ruby,Timestamp,Insert Into,我创建了一个新的迁移,其中提到 ... t.timestamps 在创建的表中添加这两列 ... | created_at | datetime | NO (Null) | | NULL (Default) | | updated_at | datetime | NO (Null) | | NULL (Default) | ... 当我想创建一个新项目时,我总是会收到错误消息 Mysql2::Error: Field '

我创建了一个新的迁移,其中提到

...
t.timestamps
在创建的表中添加这两列

...
| created_at  | datetime   | NO (Null)  |     | NULL (Default)   |                
| updated_at  | datetime   | NO (Null)  |     | NULL (Default)   |
...
当我想创建一个新项目时,我总是会收到错误消息

Mysql2::Error: Field 'created_at' doesn't have a default value: INSERT INTO `table_name` (`first_col`, `second_col`) VALUES ('a', 'b')
我错过什么了吗?我把这个小应用发送给我的一个朋友,他能够成功地运行它->记录是在数据库中创建的


我错过了什么?

我刚刚在Mac OS上新安装的MySql上遇到了类似的问题

我最终将范围缩小到新版本的MySql在默认情况下打开“严格模式”,以及我的项目有一个表,表上有一些可疑的约束。所讨论的表是
中使用的“联接表”:has\u和\u属于\u many
关系。不知何故,该表是用
:created_at
,和
:updated_at
属性创建的,这些属性上有
:null=>false的约束。Rails 3.2不会自动填充
:habtm
关系的联接表的时间戳字段。当严格模式关闭时,MySql将只使用零日期填充COL,如
0000-00-00 00:00:00
。启用严格模式后,将引发异常

为了解决这个问题,我运行了一个迁移,允许时间戳字段为空。像这样:

class ChangeNullableForTimestampsOnThing1sThing2s < ActiveRecord::Migration
  def up
    change_column(:thing1s_thing2s, :created_at, :datetime, :null => true)
    change_column(:thing1s_thing2s, :updated_at, :datetime, :null => true)
  end

  def down
    change_column(:thing1s_thing2s, :created_at, :datetime, :null => false)
    change_column(:thing1s_thing2s, :updated_at, :datetime, :null => false)
  end
end
class ChangeNullableFortimesTampsonThing1Thing2strue)
更改列(:thing1s\u thing2s,:updated\u at,:datetime,:null=>true)
结束
降下
更改列(:thing1s\u thing2s,:created\u at,:datetime,:null=>false)
更改列(:thing1s\u thing2s,:updated\u at,:datetime,:null=>false)
结束
结束

老实说,如果你不需要这些列,最好直接删除它们,但我们有几个特殊情况,它们实际上是手动设置的。

要补充Jeremy的答案,在mac电脑上,你可以用

> vi /usr/local/opt/mysql/my.cnf
然后替换

sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"


如何创建新项目?
@photo=photo.new(params[:photo])
@photo.save
非常有用,谢谢。我希望他们能尽快解决这个问题。下面是关于这个问题的更多讨论:
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"