【问题标题】:Cannot remove column that doesn't have a data type - SQLite, -Rails无法删除没有数据类型的列 - SQLite、-Rails
【发布时间】:2015-07-29 01:10:31
【问题描述】:

我的Blog 表中有这三列没有数据类型,例如: Blog(id: integer, url: text, created_at: datetime, updated_at: datetime, webname: , name: text, homeurl: , blogname: , home_url: text, blog_name: string)

其中homeurl:blogname:webname: 由于某种原因没有任何数据类型。不确定它们是如何添加到数据库中的,但我想删除它们,因为我的 Blogs 表不会显示在我的 rails 应用程序的 schema.rb 部分中。

问题是,我尝试过的所有方法都不起作用。我已经尝试rake db:migration RemoveColumn 将此作为我的迁移文件:

class RemoveColumn < ActiveRecord::Migration
  def up
    remove_column :blogs, :homeurl
  end
end

这将返回以下错误: undefined method "to_sym" for nil:NilClass/db/migrate/20150727201931_remove_columns.rb:3:in "up" NoMethodError: undefined method "to_sym" for nil:NilClass

我尝试将数据类型添加到列,我得到相同的“to_sym”错误。我也试过删除控制台中的列,同样的错误。

有没有办法删除没有数据类型的列?或者有没有办法将数据类型添加到不存在的列中?

【问题讨论】:

  • SQLite3::SQLException: near "DROP": syntax error(来自下面的评论)似乎您实际上并没有使用 MySQL。

标签: ruby-on-rails ruby sqlite


【解决方案1】:

您可以像这样将其重写为 SQL:

class RemoveColumn < ActiveRecord::Migration
  def up
    execute "ALTER TABLE blogs DROP homeurl";
  end
end  

【讨论】:

  • 我已经尝试过你的答案,但现在得到 'SQLite3::SQLException: near "DROP": syntax error: ALTER TABLE blogs DROP Homeurl/db/migrate/20150727220626_remove_column.rb:3:in @ 987654322@up' SQLite3::SQLException: 在“DROP”附近:语法错误 /db/migrate/20150727220626_remove_column.rb:3:in `up' 作为错误。
猜你喜欢
  • 1970-01-01
  • 2016-08-07
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-01
  • 2021-10-27
相关资源
最近更新 更多