无法逆转看似简单的Rails迁移-获取"altered_table.column可能不为NULL";错误 [英] Unable to reverse seemingly simple rails migration - getting "altered_table.column may not be NULL" error

查看:77
本文介绍了无法逆转看似简单的Rails迁移-获取"altered_table.column可能不为NULL";错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的开发数据库(sqlite3)中有一张发票"表,里面装有少量测试数据.

I have a table 'invoices' in my development database (sqlite3) populated with a small amount of test data.

我想在其中添加一列发票编号",并像这样设置迁移:

I wanted to add a column 'invoice_number' to it and set up a migration like so:

class AddInvoiceNumberColumnToInvoices < ActiveRecord::Migration
  def self.up
    add_column :invoices, :invoice_number, :integer
  end

  def self.down
    remove_column :invoices, :invoice_number
  end
end

我运行了rake db:migrate,它似乎可以正常迁移.但是,当我尝试通过ActiveRecord访问此列时,它似乎不存在.我决定撤消此迁移,然后使用rake db:migrate VERSION='whatever_the_migration_before_this_one_was_called'再次尝试(不知道我要尝试什么,但我认为我将从撤消它开始).失败并显示错误消息

I ran rake db:migrate and it seemed to migrate just fine. However, when I tried to access this column through ActiveRecord it didn't seem to be there. I decided to undo this migration and try again (not sure what I was going to try but I thought I'd start by undoing it) with rake db:migrate VERSION='whatever_the_migration_before_this_one_was_called'. This failed with the error message

==  AddInvoiceNumberColumnToInvoices: reverting ===============================
-- remove_column(:invoices, :invoice_number)
rake aborted!
An error has occurred, this and all later migrations canceled:

altered_invoices.invoice_number may not be NULL

我找不到此错误的任何文档.谁能解释我做错了什么,更重要的是我该如何解决?

I can't find any documentation of this error. Is anyone able to explain what I have done wrong, and more importantly how I can fix this?

推荐答案

好的,这周我终于解决了.

OK, I finally solved this weeks down the track.

我反复遇到此问题,并养成了在进行任何迁移之前备份数据库的习惯,以防我想撤消它们,因为此错误不断出现.

I repeatedly encountered this issue and got in the habit of backing up my database before doing any migrations in case I wanted to undo them as this error kept popping up.

问题是我安装了sqlite3 gem而不是sqlite3-ruby gem.我没有研究为什么会发生这种情况,但是这样做之后问题就消失了.

The issue was that I had the sqlite3 gem installed instead of the sqlite3-ruby gem. I haven't looked into why this was occurring but the problem went away after doing this;

sudo gem uninstall sqlite3
sudo gem install sqlite3-ruby

仅供参考,我正在Ruby 1.9.1上运行Rails 2.3.5

FYI I'm running Rails 2.3.5 on Ruby 1.9.1

这篇关于无法逆转看似简单的Rails迁移-获取"altered_table.column可能不为NULL";错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆