无法将rails数据库列从float迁移到整数

时间:2018-01-18 16:46:53

标签: ruby-on-rails

我正在尝试将:products 表格中:价格列的数据类型从:float 更改为:整数,但我一直收到这些错误。我已经尝试了所有的东西而且我很困难,我以为我最终会寻求帮助。

这是我的数据库迁移文件代码:

class ChangeDataTypeForProductPrice < ActiveRecord::Migration[5.1]
  def change
    change_column :products, :price, :integer
  end
end

提前感谢任何可以帮助我的人:)

这是输出

Computers-MacBook-Pro:rubyapp MyComputer$ rails db:migrate
== 20180118162844 ChangeDataTypeForProductPrice: migrating ====================
-- change_column(:products, :price, :integer)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE "products"
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108:in `step'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108:in `block in each'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:107:in `loop'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:107:in `each'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:152:in `map'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:152:in `block in execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:95:in `prepare'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:137:in `execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:251:in `block (2 levels) in execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies/interlock.rb:46:in `block in permit_concurrent_loads'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/concurrency/share_lock.rb:185:in `yield_shares'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies/interlock.rb:45:in `permit_concurrent_loads'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:250:in `block in execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:612:in `block (2 levels) in log'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:611:in `block in log'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:603:in `log'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:249:in `execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/schema_statements.rb:471:in `drop_table'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:435:in `move_table'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:427:in `block in alter_table'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:233:in `transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:426:in `alter_table'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:376:in `change_column'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:849:in `block in method_missing'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:818:in `block in say_with_time'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:818:in `say_with_time'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:838:in `method_missing'
/Users/MyComputer/Documents/Career Foundry/Achievement 3 - Basic Rails Web App/rubyapp/db/migrate/20180118162844_change_data_type_for_product_price.rb:3:in `change'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:792:in `exec_migration'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:776:in `block (2 levels) in migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:775:in `block in migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:408:in `with_connection'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:774:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:953:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1230:in `block in execute_migration_in_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1298:in `block in ddl_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/transactions.rb:210:in `transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1298:in `ddl_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1229:in `execute_migration_in_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1201:in `block in migrate_without_lock'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1200:in `each'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1200:in `migrate_without_lock'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1150:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1007:in `up'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:985:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:171:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands/rake/rake_command.rb:21:in `block in perform'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/command.rb:46:in `invoke'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
/Users/MyComputer/Documents/Career Foundry/Achievement 3 - Basic Rails Web App/rubyapp/bin/rails:9:in `require'
/Users/MyComputer/Documents/Career Foundry/Achievement 3 - Basic Rails Web App/rubyapp/bin/rails:9:in `<top (required)>'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
/Users/MyComputer/Documents/Career Foundry/Achievement 3 - Basic Rails Web App/rubyapp/bin/spring:15:in `require'
/Users/MyComputer/Documents/Career Foundry/Achievement 3 - Basic Rails Web App/rubyapp/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
ActiveRecord::InvalidForeignKey: SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE "products"
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108:in `step'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108:in `block in each'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:107:in `loop'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:107:in `each'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:152:in `map'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:152:in `block in execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:95:in `prepare'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:137:in `execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:251:in `block (2 levels) in execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies/interlock.rb:46:in `block in permit_concurrent_loads'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/concurrency/share_lock.rb:185:in `yield_shares'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies/interlock.rb:45:in `permit_concurrent_loads'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:250:in `block in execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:612:in `block (2 levels) in log'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:611:in `block in log'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:603:in `log'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:249:in `execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/schema_statements.rb:471:in `drop_table'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:435:in `move_table'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:427:in `block in alter_table'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:233:in `transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:426:in `alter_table'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:376:in `change_column'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:849:in `block in method_missing'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:818:in `block in say_with_time'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:818:in `say_with_time'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:838:in `method_missing'
/Users/MyComputer/Documents/Career Foundry/Achievement 3 - Basic Rails Web App/rubyapp/db/migrate/20180118162844_change_data_type_for_product_price.rb:3:in `change'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:792:in `exec_migration'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:776:in `block (2 levels) in migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:775:in `block in migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:408:in `with_connection'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:774:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:953:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1230:in `block in execute_migration_in_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1298:in `block in ddl_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/transactions.rb:210:in `transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1298:in `ddl_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1229:in `execute_migration_in_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1201:in `block in migrate_without_lock'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1200:in `each'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1200:in `migrate_without_lock'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1150:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1007:in `up'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:985:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:171:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands/rake/rake_command.rb:21:in `block in perform'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/command.rb:46:in `invoke'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
/Users/MyComputer/Documents/Career Foundry/Achievement 3 - Basic Rails Web App/rubyapp/bin/rails:9:in `require'
/Users/MyComputer/Documents/Career Foundry/Achievement 3 - Basic Rails Web App/rubyapp/bin/rails:9:in `<top (required)>'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
/Users/MyComputer/Documents/Career Foundry/Achievement 3 - Basic Rails Web App/rubyapp/bin/spring:15:in `require'
/Users/MyComputer/Documents/Career Foundry/Achievement 3 - Basic Rails Web App/rubyapp/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
SQLite3::ConstraintException: FOREIGN KEY constraint failed
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108:in `step'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108:in `block in each'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:107:in `loop'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:107:in `each'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:152:in `map'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:152:in `block in execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:95:in `prepare'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:137:in `execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:251:in `block (2 levels) in execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies/interlock.rb:46:in `block in permit_concurrent_loads'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/concurrency/share_lock.rb:185:in `yield_shares'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies/interlock.rb:45:in `permit_concurrent_loads'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:250:in `block in execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:612:in `block (2 levels) in log'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:611:in `block in log'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:603:in `log'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:249:in `execute'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/schema_statements.rb:471:in `drop_table'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:435:in `move_table'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:427:in `block in alter_table'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:233:in `transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:426:in `alter_table'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:376:in `change_column'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:849:in `block in method_missing'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:818:in `block in say_with_time'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:818:in `say_with_time'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:838:in `method_missing'
/Users/MyComputer/Documents/Career Foundry/Achievement 3 - Basic Rails Web App/rubyapp/db/migrate/20180118162844_change_data_type_for_product_price.rb:3:in `change'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:792:in `exec_migration'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:776:in `block (2 levels) in migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:775:in `block in migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:408:in `with_connection'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:774:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:953:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1230:in `block in execute_migration_in_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1298:in `block in ddl_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/transactions.rb:210:in `transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1298:in `ddl_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1229:in `execute_migration_in_transaction'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1201:in `block in migrate_without_lock'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1200:in `each'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1200:in `migrate_without_lock'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1150:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:1007:in `up'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/migration.rb:985:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:171:in `migrate'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands/rake/rake_command.rb:21:in `block in perform'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/command.rb:46:in `invoke'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
/Users/MyComputer/Documents/Career Foundry/Achievement 3 - Basic Rails Web App/rubyapp/bin/rails:9:in `require'
/Users/MyComputer/Documents/Career Foundry/Achievement 3 - Basic Rails Web App/rubyapp/bin/rails:9:in `<top (required)>'
/Users/MyComputer/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'

5 个答案:

答案 0 :(得分:1)

根据SQLite文档:https://www.sqlite.org/lang_altertable.html,可以更改表的唯一操作是重命名和添加列。其他所有内容(包括在这种情况下更改列)都需要一个创建新表并删除旧表的事务。

在这种情况下,您无法删除该表,因为您在要删除的表上有另一个带有外键约束的表。

一种方法是添加具有所需类型的新列,并对整个表执行完全更新以将float复制到整数列,然后对代码进行适当的更改。或者假设这是开发,删除依赖于产品表的数据并再次尝试迁移。

答案 1 :(得分:0)

您可以创建新的colunm并使用价格列数据更新它,稍后您可以删除旧列并将新列重命名为价格。作为一个有效的预防措施,您可以将浮点值转换为整数并更新新创建的列。

答案 2 :(得分:0)

您只需创建一个迁移以删除列:products,然后创建一个新的迁移,从头开始创建一个整数类型的列。

答案 3 :(得分:0)

尝试以下

rails g migration change_float_to_integer

然后project/db/migrate/TIMESTAMP_change_float_to_integer.rb

如下编辑

class ChangeFloatToInteger< ActiveRecord::Migration[5.0]
    def change
        remove_column :products, :price

        add_column :products, :price, :integer
    end
end

然后

rake db:migrate

希望能提供帮助

答案 4 :(得分:0)

运行db:reset然后突然一切正常。啊。我想这个数据库已经被某些东西搞砸了。