数据库重置后,列'sender_id'不能为空

时间:2013-04-05 19:03:06

标签: ruby-on-rails

我正在尝试修复在执行rake db之后发生的一些新错误:migrate:reset。我已经修好了所有这些,除了这个。对于我的自定义收件箱,在我点击发送新邮件后,我收到此错误:

  

Mysql2 ::错误:列'sender_id'不能为null:INSERT INTO   messagesbodycontainercreated_atread_at,   recepient_deletedrecepient_idsender_deletedsender_id,   subjectupdated_at)VALUES('test 1','draft','2013-04-05   18:59:08',NULL,0,16,0,NULL,'test 1','2013-04-05 18:59:08')

现在有一些背景资料。在我重置之前,我改变了迁移。那么也许这会发挥作用?

原始迁移:

class CreateMessages < ActiveRecord::Migration
    def up
        create_table :messages do |t|
            t.string :sender_id,:null => false
            t.string :recepient_id
            t.boolean :sender_deleted, :recepient_deleted, :default => false
            t.string :subject,:null => false
            t.text :body
            t.datetime :read_at
            t.string :container,:default => "draft"
            t.timestamps
        end
    end

    def down
        drop_table :messages
    end
end

修改后的迁移,所以我可以从字符串更改为整数:

class ChangeSenderIdAndRecepientIdColumns < ActiveRecord::Migration
  def change
    change_column :messages, :sender_id, :integer
    change_column :messages, :recepient_id, :integer
  end

  def down
     change_column :messages, :sender_id, :string
     change_column :messages, :recepient_id, :string
  end
end

消息控制器:

  def create
    @message = Message.new(params[:message])
    @message.sender_id = @user_id
    if @message.save
      flash[:notice] = "Message has been sent"
      redirect_to user_messages_path(current_user, :mailbox=>:inbox)
    else
      render :action => :new
    end
  end

0 个答案:

没有答案