Changing column to NOT NULL with migration silently failing

Hi

I’ve created a migration called
make_customer_type_id_not_null_for_customers. The migration .rb file
looks like this:

class MakeCustomerTypeIdNotNullForCustomers < ActiveRecord::Migration
def self.up
change_column(:customers, :customer_type_id, :integer, { :null =>
false })
end

def self.down
change_column(:customers, :customer_type_id, :integer, { :null =>
true })
end
end

Running “rake migrate” indicates everything is ok:

== MakeCustomerTypeIdNotNullForCustomers: migrating

– change_column(:customers, :customer_type_id, :integer,
{:null=>false})
-> 0.1250s
== MakeCustomerTypeIdNotNullForCustomers: migrated (0.1250s)

However, this is misleading, since the column was in fact not changed,
and Null values are still accepted, seriously compromising data
integrity.

Has anyone encountered this problem? Is it caused by any mistake I
can fix? The problem exists on Rails 1.1.2 with PostgresSQL 8.1.3 on
both Windows and Debian Stable.

== MakeCustomerTypeIdNotNullForCustomers: migrating

– change_column(:customers, :customer_type_id, :integer,
{:null=>false})
-> 0.1250s
== MakeCustomerTypeIdNotNullForCustomers: migrated (0.1250s)

However, this is misleading, since the column was in fact not changed,
and Null values are still accepted, seriously compromising data
integrity.

Has anyone encountered this problem? Is it caused by any mistake I
can fix? The problem exists on Rails 1.1.2 with PostgresSQL 8.1.3 on
both Windows and Debian Stable.
That’s odd.

Changing a column on mysql 5 with Rails 1.1.2 produces this SQL:

this:
change_column :partners, :name, :string, {:null => false}
produces:
SQL (0.444450) ALTER TABLE partners CHANGE name name varchar(255) NOT
NULL

this:
change_column :partners, :name, :string, {:null => true}
produces:
SQL (0.419012) ALTER TABLE partners CHANGE name name varchar(255)
DEFAULT ‘’

What does AR::Schema generate for Postgres in your setup? What’s in
development.log when that migration executes?

-damon
http://damonclinkscales.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs