This project is archived and is in readonly mode.

#6405 ✓invalid
ronin-68998 (at lighthouseapp)

Delete me

Reported by ronin-68998 (at lighthouseapp) | February 9th, 2011 @ 11:19 PM

Oh nevermind

Comments and changes to this ticket

  • ronin-68998 (at lighthouseapp)

    ronin-68998 (at lighthouseapp) February 10th, 2011 @ 12:59 AM

    Found the issue:

    This should at least raise a warning or be documented that you're doing something silly or non-conventional.

    Bug:

    Given the following migration you'll notice two columns with :null => false, in the following model 'Broken' you'll notice one validates_presence_of.

    If you run the following code:

    Broken.create({:first_name => 'Test', :last_name => 'User'})
    => PGError: ERROR:  null value in column "first_name" violates not-null constraint
    
    Works.create({:first_name => 'Test', :last_name => 'User'})
    => YAY
    

    :last_name is ignored before insert because it's not in the validator chain, that's clearly undocument or faulty behavor for postgresql, even though the value for :last_name was passed.

    class CreateUsers < ActiveRecord::Migration
      def self.up
        create_table :users do |t|
    
          # names!
          t.string :first_name,             :null => false
          t.string :last_name,              :null => false
    
          t.timestamps
        end
      end
    
      def self.down
        drop_table :users
      end
    end
    
    class Works < ActiveRecord::Base
      set_table_name 'users'
    
      validates_presence_of :first_name, :on => :create
      validates_presence_of :last_name, :on => :create
    
    end
    
    class Broken < ActiveRecord::Base
      set_table_name 'users'
    
      validates_presence_of :first_name, :on => :create
    
    end
    
  • ronin-68998 (at lighthouseapp)

    ronin-68998 (at lighthouseapp) February 10th, 2011 @ 01:26 AM

    • Title changed from “ActiveRecord create on Postgres induces not-null constrains” to “Delete me”
  • Rohit Arondekar

    Rohit Arondekar February 10th, 2011 @ 05:42 AM

    • State changed from “new” to “invalid”
    • Importance changed from “” to “Low”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages