Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Major
-
Resolution: Incomplete
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
-
Environment:MacOS X 10.4.11, Java 1.5.0_13, JRuby trunk r5512, rails 2.0.2, activerecord-jdbc-adapter 0.7.1, activerecord-jdbcderby-adapter 0.7.1
-
Number of attachments :
Description
Migrating a set of migrations from mysql to derby I found that I couldn't create columns in a derby database with a 'NOT NULL' constraint unless I also set a default value.
It is common in rails apps to set ":null => false" as a column constraint without specifying a default value.
Perhaps this is not an error in the jdbcderby adaptor but instead a function of how Derby itself operates.
Here's a simple test case.
Create a simple rails testapp:
$ rails testapp --database mysql
Edit config/database.yml to use derby instead of mysql:
$ cat config/database.yml development: adapter: jdbcderby database: db/development.derby timeout: 5000 test: adapter: jdbcderby database: db/test.derby timeout: 5000 production: adapter: jdbcderby database: db/production.derby timeout: 5000
Create a scaffold with a migration:
$ jruby script/generate scaffold widget name:string description:text
Here's what the schem looks like now:
$ cat db/schema.rb ActiveRecord::Schema.define(:version => 1) do create_table "widgets", :force => true do |t| t.string "name" t.text "description" t.timestamp "created_at" t.timestamp "updated_at" end end
Create a new migration adding a column:
$ cat db/migrate/002_add_field.rb
class AddField < ActiveRecord::Migration
def self.up
add_column :widgets, :color, :string, :null => false
end
def self.down
remove_column :widgets, :function
end
end
The migration fails:
$ rake db:migrate
(in /Users/stephen/dev/jruby/rails/testapp)
== 2 AddField: migrating ======================================================
-- add_column(:widgets, :color, :string, {:null=>false})
rake aborted!
ActiveRecord::ActiveRecordError: In an ALTER TABLE statement, the column 'COLOR' has been specified as NOT NULL and either the DEFAULT clause was not specified or was specified as DEFAULT NULL.: ALTER TABLE widgets ADD color varchar(256) NOT NULL
If instead a default value is added to the new column definition:
add_column :widgets, :color, :string, :null => false, :default => 'red'
The migration succeeds.
However initially creating a table with a column with a 'NOT NULL' constraint and with no default value does work.
Here's the sole migration on the new test now:
[~/dev/jruby_trunk/jruby/rails/testapp]$ cat db/migrate/001_create_widgets.rb class CreateWidgets < ActiveRecord::Migration def self.up create_table :widgets do |t| t.string :name t.text :description t.string :color, :null => false t.timestamps end end def self.down drop_table :widgets end endThe migration now runs successfully:
Here's the complete schema: