开发者

Heroku ActiveRecord Error: PgSQL put null in id field

开发者 https://www.devze.com 2023-04-08 17:14 出处:网络
I just deployed a simple Ruby on Rails (3.0.10) application to Heroku. There is a line of code create a new User object like this.

I just deployed a simple Ruby on Rails (3.0.10) application to Heroku. There is a line of code create a new User object like this.

User.create(:name => "[name here]", :email => "[email here]")

It can run well in my local machine, which is using MySQL. After I deployed it to Heroku, I got an error.

ActiveRecord::StatementInvalid: PGError: ERROR:  null value in column "id" violates not-n开发者_如何学运维ull constraint : INSERT INTO "users" ("id", "name", "email", "created_at", "updated_at") VALUES (NULL, '[name here]', '[email here]', '2011-09-28 03:59:12.908593', '2011-09-28 03:59:12.908593') RETURNING "id"

I have no idea what's wrong with my code. Did i miss anything?

Thanks all.

UPDATE

Migration

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :email
      t.string :name

      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

Schema

ActiveRecord::Schema.define(:version => 20110922071106) do

  create_table "users", :force => true do |t|
    t.string   "email"
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end


Did you do this: (Taken from http://railsapps.github.com/rails-heroku-tutorial.html)

Replace SQLite with PostgreSQL If you are developing locally using SQLite, you will need to switch to PostgreSQL for deployment to Heroku. If you don’t want to develop using PostgreSQL locally, you can set up your Gemfile to use SQLite for development and PostgreSQL for production.

To switch from SQLite to PostgreSQL for deployment to Heroku, edit your Gemfile and change this line:

gem 'sqlite3'

To this:

group :production do
  gem 'pg' 
end 
group :development, :test do
  gem 'sqlite3'
end


For anyone else looking, I had this same problem. Worked fine in dev, but failed in production with Postgres. The problem I had I tracked back to the CanCan plugin, specifically code I had that was creating a guest user the ability.rb file, if no user object existed. The guest account was suggested in a Railscast. I removed the User.new guest creation command and the problem resolved.


I just re-create the whole RoR application, and copy all the controllers, models, and views that I built to the new app. It is now running well.

I tried to compare 2 versions and didn't have any result. Will let you guys know if I find out the cause of this.

Thanks all. :)


I had a similar problem. if you try to reload the schema (on local, dont do it on production obviously), you should see a difference in what you think you have and what the database actually thinks.

rake db:schema:dump
rake db:schema:load

THIS WILL ERASE YOUR DATA. But you should see a difference in the schema for 'users'. This was my difference:

The fresh dump showed:

create_table "posts", :id => :false, :force => true do |t|
    t.integer "id", :null  => false, :limit=> 8
    t.integer  "object_id",    :limit => 8
    t.string   "post_type"
    t.string   "from_id"
    t.text     "picture_url"
    t.text     "video_source"
    t.integer  "shares"
    t.integer  "likes"
    t.datetime "created_at",                :null => false
    t.datetime "updated_at",                :null => false
    t.integer  "image_size",   :limit => 8
    t.integer  "vid_size",     :limit => 8
end

But i know that id is handled by rails and this should be:

create_table "posts", :force => true do |t|
    t.integer  "object_id",    :limit => 8
    t.string   "post_type"
    t.string   "from_id"
    t.text     "picture_url"
    t.text     "video_source"
    t.integer  "shares"
    t.integer  "likes"
    t.datetime "created_at",                :null => false
    t.datetime "updated_at",                :null => false
    t.integer  "image_size",   :limit => 8
    t.integer  "vid_size",     :limit => 8
end

If this is also the case for you, simply add to your user model:

set_primary_key :id

That reset it and it works now. I still don't know the cause of the problem but this fixed it for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号