开发者

DataMapper migrating column to a new data type

开发者 https://www.devze.com 2023-02-09 20:52 出处:网络
Issue is that I can\'t change a column type to \'Text\' This is the error I am seeing: DarkBook:playground Justin$ rake migrate_up 
(in /Users/Justin/Dropbox/Business/datamapper/playground) 
 ~

Issue is that I can't change a column type to 'Text'

This is the error I am seeing:

DarkBook:playground Justin$ rake migrate_up 
(in /Users/Justin/Dropbox/Business/datamapper/playground) 
 ~ Starting Migration 
 == Performing Up Migration #1: create_person_table 
   CREATE TABLE people (id SERIAL PRIMARY KEY, name VARCHAR(2), 
age INTEGER) ENGINE = InnoDB CHARACTER SET utf8 COLLATE 
utf8_general_ci 
   -> 0.0112s 
 -> 0.0129s 
 == Performing Up Migration #2: change_name_constraints 
rake aborted! 
uninitialized constant SQL::TableModifier::Text 
/Users/Justin/Dropbox/Business/datamapper/playground/rakefile.rb:61:in 
`block (3 levels) in ' 
(See full trace by running task with --trace) Rake script I'm using to test DataMapper: 
https://gist.github.com/818143 It seems as though the change_column method within the 
TableModifier class isn't converting the column change statement to 
MySQL correctly. Other seemingly related errors are that if I try to 
convert a column to a String of longer length I get this: DarkBook:playground Justin$ rake migrate_up 
(in /Users/Justin/Dropbox/Business/datamapper/playground) 
 ~ Starting Migration 
 == Performing Up Migration #2: change_name_constraints 
   

ALTER TABLE people ALTER COLUMN name TYPE String 
 

~ You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use 
near 'TYPE String' at line 1 (code: 1064, sql state: 42000, query: 
ALTER TABLE people ALTER COLUMN name TYPE String, uri: 
mysql://root:@127.0.0.1datamapper_test) 
rake aborted! 
You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use 
near 'TYPE String' at line 1 


/Users/Justin/Dropbox/Business/datamapper/playground/rakefile.rb:60:in 
`block (2 levels) in ' 
(See full trace by running task with --trace)

According to what I've read about MySQL, "ALTER TABLE people ALTER 
COLUMN name TYPE String" is not a valid MySQL query command. TYPE 
shouldn't be there and String should be converted to VARCHAR.

Looking inside the change_column m开发者_如何学运维ethod I see it's just a straight 
pass through of the type I'm providing. This is making me think 
perhaps I'm not supposed to be calling the change_column method 
directly?

Once I get this figured out and I'm well on my way, I'm documenting 
this stuff as figuring out DataMapper's migration api has been 
hellish.


change_column isn't particularly bright. The current implementation is:

def change_column(name, type, opts = {})
  @statements << "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(name)} TYPE #{type}"
end

Meaning you need to specify the underlying sql type rather than a DataMapper type. To wit:

modify_table :people do
  change_column :name, 'text'
end

I will update the documentation to include this.


This is a work around I found: https://gist.github.com/819792

0

精彩评论

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

关注公众号