ALTER TABLE failure using jdbcderby during rails migration adding a column with
'not null' constraint
-----------------------------------------------------------------------------------------------------
Key: JRUBY-1905
URL: http://jira.codehaus.org/browse/JRUBY-1905
Project: JRuby
Issue Type: Bug
Components: ActiveRecord-JDBC
Affects Versions: JRuby 1.1
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
Reporter: Stephen Bannasch
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:
{code}
$ rails testapp --database mysql
{code}
Edit config/database.yml to use derby instead of mysql:
{code}
$ 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
{code}
Create a scaffold with a migration:
{code}
$ jruby script/generate scaffold widget name:string description:text
{code}
Here's what the schem looks like now:
{code}
$ 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
{code}
Create a new migration adding a column:
{code}
$ 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
{code}
The migration fails:
{code}
$ 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
{code}
If instead a default value is added to the new column definition:
{code}
add_column :widgets, :color, :string, :null => false, :default => 'red'
{code}
The migration succeeds.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email