Rails 迁移错误
这看起来很简单,但我不确定出了什么问题。
我正在尝试在 Rails 迁移中执行以下操作:
change_column :foo, :bar, :text, :limit => 16777215
我收到以下错误
Mysql::Error: BLOB/TEXT column 'bar' can't have a default value: ALTER TABLE `foo` CHANGE `bar` `email_contents` text(16777215) DEFAULT '' NOT NULL
我唯一能想到的是导致问题的是,在我将列添加到 foo 并必须将其更改为首先输入 :string 来输入 :text 。这些都来自自己的迁移脚本,如下所示:
add_column :foo, :bar, :string, :null => false
作为
change_column :foo, :bar, :text
一个实验,我尝试更改第一个change_column(change_column:foo,:bar,:text)并发现这成功地更改了表。不幸的是,我无法更改之前的任何一个迁移,只能在当前的实现中添加新的迁移,这样就无法在生产中工作。问题是,是什么让我可以更改列一次而不是两次?
更新尝试了第一个建议,但得到以下结果:
Mysql::Error: BLOB/TEXT column 'bar' can't have a default value: ALTER TABLE `foo` CHANGE `bar` `bar` text(16777215) DEFAULT ''
This seems pretty straight forward but I'm not sure what is going wrong.
I'm attempting to do the following in my Rails migration:
change_column :foo, :bar, :text, :limit => 16777215
I'm getting the following error
Mysql::Error: BLOB/TEXT column 'bar' can't have a default value: ALTER TABLE `foo` CHANGE `bar` `email_contents` text(16777215) DEFAULT '' NOT NULL
The only thing I can figure is causing an issue is that this change_column is occurring shortly after I added the column to foo and had to change it from type :string to type :text in the first place. These each come from their own migration scripts and look like this:
add_column :foo, :bar, :string, :null => false
and
change_column :foo, :bar, :text
As an experiment, I tried changing the first change_column (change_column :foo, :bar, :text) and discovered that this successfully alters the table. Unfortunately I cannot change either of the previous migrations and can only add new ones in our current implementation so that will not work in production. The question is, what is allowing me to change the column once but not twice?
Update Tried the first suggestion but got the following:
Mysql::Error: BLOB/TEXT column 'bar' can't have a default value: ALTER TABLE `foo` CHANGE `bar` `bar` text(16777215) DEFAULT ''
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试
change_column :foo, :bar, :text, :limit => 16777215,:默认=>零,:空=>正确
try
change_column :foo, :bar, :text, :limit => 16777215, :default => nil, :null => true
如果有人看到这篇文章并发现它有用。我遇到了同样的问题,另一种避免方法是更改 mysql 配置,以便 sql-mode 不严格,即不包含默认情况下的 STRICT_TRANS_TABLES 。
In case someone comes across this post and find this useful. I had the same problem the other way of avoid it is to change mysql configuration so that sql-mode is not strict, i.e. doesn't include
STRICT_TRANS_TABLES
which it does by default.您是否还有其他使用
:text
指定:limit
的迁移?如果我正确理解的话,
:text
可能不接受:limit
,它只是映射到特定的 MySQL 数据类型。Rails 迁移类型和 MySQL 数据类型的映射:
http://www.orthogonalthought.com/blog/index.php/2007/06/mysql-and-ruby-on-rails-datatypes/" orthogonalthought.com/blog/index.php/2007/06/mysql-and-ruby-on-rails-datatypes/
MySQL TEXT 类型(没有提到 LIMIT,但我想并不排除它:
http://dev.mysql.com/doc/refman /5.0/en/string-type-overview.html
Do you have other migrations where a
:limit
is specified with:text
that work?Possible that
:text
doesn't accept a:limit
, and it simply maps to a specific MySQL datatype, if I read this correctly.Map of Rails migration types, and MySQL data types:
http://www.orthogonalthought.com/blog/index.php/2007/06/mysql-and-ruby-on-rails-datatypes/
MySQL TEXT types (no mention of LIMIT, but doesn't rule it out I suppose:
http://dev.mysql.com/doc/refman/5.0/en/string-type-overview.html
对我来说,这似乎是从 MySQL 5.5.x 到 5.6.x 的结果
注意:有人应该去阅读 Semantic版本 2.0.0
我的修复非常简单...
WAS
IS
For me this seems to be a result of going from MySQL 5.5.x to 5.6.x
Note: Someone should go read Semantic Versioning 2.0.0
My fix was pretty simple...
WAS
IS
试试这个:
当
:null =>使用 false(如旧迁移中一样),Rails 将 DEFAULT 位添加到 ALTER TABLE 语句中。但是,正如错误所述,TEXT 列不能有 DEFAULT。将其更改为
:null => true
在新的迁移中问题应该消失。Try this:
When
:null => false
is used (as in the old migration), Rails adds the DEFAULT bit to the ALTER TABLE statement. But, like the error says, TEXT columns can't have a DEFAULT. By changing it to:null => true
in the new migration the problem should go away.这对我有用:
我必须添加
default: nil
,然后 Rails 可以设置null: true
,这会删除早期迁移中定义的默认值。This worked for me:
I had to add
default: nil
and then Rails was OK settingnull: true
, which removes the default defined in an earlier migration.