Rails 迁移错误

发布于 2024-11-06 12:11:09 字数 908 浏览 5 评论 0原文

这看起来很简单,但我不确定出了什么问题。

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

也只是曾经 2024-11-13 12:11:09

尝试

change_column :foo, :bar, :text, :limit => 16777215,:默认=>零,:空=>正确

try

change_column :foo, :bar, :text, :limit => 16777215, :default => nil, :null => true

温柔少女心 2024-11-13 12:11:09

如果有人看到这篇文章并发现它有用。我遇到了同样的问题,另一种避免方法是更改​​ 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.

薄荷港 2024-11-13 12:11:09

您是否还有其他使用 :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

帅哥哥的热头脑 2024-11-13 12:11:09

对我来说,这似乎是从 MySQL 5.5.x 到 5.6.x 的结果

注意:有人应该去阅读 Semantic版本 2.0.0

我的修复非常简单...

WAS

change_column :my_table, :my_column, :mediumtext #=> Migrations Explosion

IS

change_column :my_table, :my_column, :mediumtext, default: nil #=> All good in the mysql 5.6.21 

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

change_column :my_table, :my_column, :mediumtext #=> Migrations Explosion

IS

change_column :my_table, :my_column, :mediumtext, default: nil #=> All good in the mysql 5.6.21 
月光色 2024-11-13 12:11:09

试试这个:

change_column :foo, :bar, :text, :limit => 16777215, :null => true

:null =>使用 false(如旧迁移中一样),Rails 将 DEFAULT 位添加到 ALTER TABLE 语句中。但是,正如错误所述,TEX​​T 列不能有 DEFAULT。将其更改为 :null => true 在新的迁移中问题应该消失。

Try this:

change_column :foo, :bar, :text, :limit => 16777215, :null => true

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.

话少心凉 2024-11-13 12:11:09

这对我有用:

    change_column :delayed_jobs, :handler, :text, 
      limit: 16777215, null: true, default: nil

我必须添加 default: nil ,然后 Rails 可以设置 null: true ,这会删除早期迁移中定义的默认值。

This worked for me:

    change_column :delayed_jobs, :handler, :text, 
      limit: 16777215, null: true, default: nil

I had to add default: nil and then Rails was OK setting null: true, which removes the default defined in an earlier migration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文