waka / activerecord-mysql-unsigned Goto Github PK
View Code? Open in Web Editor NEWEnable to use the "unsigned" option in the integer type for migrating of ActiveRecord
License: MIT License
Enable to use the "unsigned" option in the integer type for migrating of ActiveRecord
License: MIT License
It'd be nice if the gem automatically made integer columns have validations (via introspection), e.g.:
validates column, numericality: { only_integer: true, greater_than_or_equal_to: 0, presence: !column_allows_nil }
Rails/Ruby novice here. I tried to use your project recently and ran into the following exception:
Versions
-- create_table(:ipv6_locations)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead: CREATE TABLE `ipv6_locations` (`id` int(10) unsigned DEFAULT NULL auto_increment PRIMARY KEY, `ip_from` decimal(39,0) NOT NULL, `ip_to` decimal(39,0) NOT NULL, `country` varchar(2) NOT NULL, `region` varchar(128) NOT NULL, `city` varchar(128) NOT NULL) ENGINE=InnoDB
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `_query'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `block in query'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/mysql2-0.3.21/lib/mysql2/client.rb:79:in `handle_interrupt'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/mysql2-0.3.21/lib/mysql2/client.rb:79:in `query'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/rack-mini-profiler-0.10.5/lib/patches/db/mysql2.rb:20:in `query'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `block in execute'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract_adapter.rb:378:in `block in log'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/activesupport-4.1.16/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract_adapter.rb:372:in `log'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `execute'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/activerecord-4.1.16/lib/active_record/connection_adapters/mysql2_adapter.rb:228:in `execute'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract/schema_statements.rb:205:in `create_table'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:460:in `create_table'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/activerecord-4.1.16/lib/active_record/migration.rb:656:in `block in method_missing'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/activerecord-4.1.16/lib/active_record/migration.rb:628:in `block in say_with_time'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/activerecord-4.1.16/lib/active_record/migration.rb:628:in `say_with_time'
/home/vagrant/.rvm/gems/ruby-2.2.5/gems/activerecord-4.1.16/lib/active_record/migration.rb:648:in `method_missing'
Next I tried "fixing" the primary key definition by following answers suggested here, which didn't work. I then realised that this gem was the one causing the error.
create_table :table_name do |t|
t.references :another_table_name
end
creates a signed int(4) field.
btw, why all the columns without :limit option are created as int(4) rather than default int(11)?
rails 4.1.6
activerecord-mysql-unsigned 0.0.3
if use
t.integer "level", default: 0, limit: 1
export:
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
wrong number of arguments (4 for 5)/Users/archer/.rvm/gems/ruby-2.1.2/gems/activerecord-mysql-unsigned-0.0.3/lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract_adapter.rb:21:in `type_to_sql'......
When I run this migration.
def change
add_column 'blogs', :role, :string, after: 'user_id'
end
It seems 'after' option didn't work.
'role' column is added to the tail of 'blogs' table.
As far as I can tell, all ActiveRecord int
columns are set with display width of 11.
Example below modified from Rails Guides:
CREATE TABLE products (
id int(11) NOT NULL auto_increment,
val int(11) NOT NULL,
name varchar(255),
PRIMARY KEY (id)
);
This makes sense with the MySQL default for signed int
, since it requires 11 characters to display the min value and the negative sign. See this SO question.
Currently, the gem modifies the default behavior and all int
value columns are created with a display width of 10. This causes slight differences in the structure.sql
files created by ActiveRecord. In the spec migrations you refer to the int(10)
being the default ActiveRecord primary_key, but that does not appear to be true.
Does it make more sense to respect the default ActiveRecord behavior and for signed int
columns set the display width to 11 and for unsigned int
columns set it to 10?
CREATE TABLE products (
id int(10) unsigned NOT NULL auto_increment,
val int(11) NOT NULL,
n int(10) unsigned NOT NULL,
name varchar(255),
PRIMARY KEY (id)
);
SEE ALSO: https://travis-ci.org/walf443/activerecord-mysql-unsigned/builds/48597097
it may be caused that activerecord changed ChangeColumnDefinition class definition since rails/rails@3c10043bc27.
Not working for rails 4.0.3.
Bundler could not find compatible versions for gem "activerecord":
In Gemfile:
rails (= 4.0.3) ruby depends on
activerecord (= 4.0.3) ruby
activerecord-mysql-unsigned (= 0.0.2) ruby depends on
activerecord (3.2.17)
When trying to run rake db:reset
, not add unsigned option.
I would be nice to have this supported by activerecord-mysql-unsigned.
Rails 4.0.2
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin12.5.0]
mysql2 0.3.15
activerecord-mysql-unsigned 0.0.3
こんにちは。
日本語で失礼します。
先ほど、v0.3.0のソースをgithubから落としてbundle rake rake
を実行したのですが、change_column
が正しく動いていないように見えます。
具体的には、以下の箇所でSQL文を標準出力に出したところ
-- change_column(:users, :id, :integer, {:limit=>8, :unsigned=>true, :null=>false, :auto_increment=>true})
D, [2014-11-24T01:59:46.458223 #2196] DEBUG -- : (0.2ms) ALTER TABLE `users`
-> 0.0027s
-- change_column(:users, :will_unsigned_int, :integer, {:unsigned=>true})
D, [2014-11-24T01:59:46.461271 #2196] DEBUG -- : (0.2ms) ALTER TABLE `users`
-> 0.0030s
-- change_column(:users, :will_signed_int, :integer, {:unsigned=>false})
D, [2014-11-24T01:59:46.465085 #2196] DEBUG -- : (0.2ms) ALTER TABLE `users`
-> 0.0037s
-- change_column(:users, :will_bigint, :integer, {:limit=>8})
D, [2014-11-24T01:59:46.468309 #2196] DEBUG -- : (0.2ms) ALTER TABLE `users`
-> 0.0031s
-- add_column(:users, :added_unsigned_int, :integer, {:unsigned=>true})
D, [2014-11-24T01:59:46.468919 #2196] DEBUG -- : (0.2ms) ALTER TABLE `users`
上記のようにALTER文が正常に実行されておらず、テーブルの定義を見ても型の変更は行われていませんでした。
v0.2.0のソースで上記の箇所をログ出力すると
-- change_column(:users, :id, :integer, {:limit=>8, :unsigned=>true, :null=>false, :auto_increment=>true})
D, [2014-11-24T02:02:14.542172 #2310] DEBUG -- : (17.6ms) ALTER TABLE `users` CHANGE `id` `id` bigint unsigned NOT NULL AUTO_INCREMENT
-> 0.0204s
-- change_column(:users, :will_unsigned_int, :integer, {:unsigned=>true})
D, [2014-11-24T02:02:14.558033 #2310] DEBUG -- : (12.6ms) ALTER TABLE `users` CHANGE `will_unsigned_int` `will_unsigned_int` int(10) unsigned DEFAULT NULL
-> 0.0158s
-- change_column(:users, :will_signed_int, :integer, {:unsigned=>false})
D, [2014-11-24T02:02:14.584018 #2310] DEBUG -- : (22.8ms) ALTER TABLE `users` CHANGE `will_signed_int` `will_signed_int` int(4) DEFAULT NULL
-> 0.0259s
-- change_column(:users, :will_bigint, :integer, {:limit=>8})
D, [2014-11-24T02:02:14.598764 #2310] DEBUG -- : (11.1ms) ALTER TABLE `users` CHANGE `will_bigint` `will_bigint` int(8) DEFAULT NULL
-> 0.0147s
-- add_column(:users, :added_unsigned_int, :integer, {:unsigned=>true})
D, [2014-11-24T02:02:14.619403 #2310] DEBUG -- : (20.3ms) ALTER TABLE `users` ADD `added_unsigned_int` int(10) unsigned
-> 0.0206s
正常にALTER文が発行されていることが確認できました。
テストの実行環境は以下の通りです。
$ ruby -v
ruby 2.0.0p594 (2014-10-27 revision 48167) [x86_64-darwin13.4.0]
$ bundle list
Gems included by the bundle:
* activemodel (4.1.8)
* activerecord (4.1.8)
* activerecord-mysql-unsigned (0.2.0)
* activesupport (4.1.8)
* arel (5.0.1.20140414130214)
* builder (3.2.2)
* bundler (1.7.4)
* database_cleaner (1.3.0)
* diff-lcs (1.2.5)
* i18n (0.6.11)
* json (1.8.1)
* minitest (5.4.3)
* mysql2 (0.3.17)
* rake (10.3.2)
* rspec (3.0.0)
* rspec-core (3.0.4)
* rspec-expectations (3.0.4)
* rspec-mocks (3.0.4)
* rspec-support (3.0.4)
* thread_safe (0.3.4)
* tzinfo (1.2.2)
$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 41
Server version: 5.5.40-log Source distribution
私の勘違いでしたら大変申し訳ないのですが、ご確認いただければ幸いです… 🙇
It seems this gem does not support Rails 5.
Any chance for an upgrade?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.