#ruby-on-rails #mysql2
Вопрос:
когда я следовал учебнику, я столкнулся с ошибкой при выполнении миграции, которую я не мог понять. при переносе кода, который я написал для отслеживаемого устройства, произошла ошибка. было бы полезно, если бы вы могли дать ответ в виде кода с объяснением, так как я новичок, спасибо.
отслеживаемый
class AddTrackableToDevise < ActiveRecord::Migration[6.1]
def up
unless column_exists?(:users, :sign_in_count)
add_column :users, :sign_in_count, :integer, default: 0, null: false
end
unless column_exists?(:users, :current_sign_in_at)
add_column :users, :current_sign_in_at, :datetime
end
unless column_exists?(:users, :last_sign_in_at)
add_column :users, :last_sign_in_at, :datetime
end
unless column_exists?(:users, :current_sign_in_ip)
add_column :users, :current_sign_in_ip, :inet
end
unless column_exists?(:users, :last_sign_in_ip)
add_column :users, :last_sign_in_ip, :inet
end
end
def down
remove_columns :users, :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip
end
end
ошибка
== 20210817054855 AddTrackableToDevise: migrating =============================
-- column_exists?(:users, :sign_in_count)
-> 0.0029s
-- column_exists?(:users, :current_sign_in_at)
-> 0.0023s
-- column_exists?(:users, :last_sign_in_at)
-> 0.0020s
-- column_exists?(:users, :current_sign_in_ip)
-> 0.0020s
-- add_column(:users, :current_sign_in_ip, :inet)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: 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 'inet' at line 1
/home/user/ruby_projects/ruby_auth/authapp/db/migrate/20210817054855_add_trackable_to_devise.rb:13:in `up'
/home/user/ruby_projects/ruby_auth/authapp/bin/rails:5:in `<top (required)>'
/home/user/ruby_projects/ruby_auth/authapp/bin/spring:10:in `block in <top (required)>'
/home/user/ruby_projects/ruby_auth/authapp/bin/spring:7:in `<top (required)>'
Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: 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 'inet' at line 1
/home/user/ruby_projects/ruby_auth/authapp/db/migrate/20210817054855_add_trackable_to_devise.rb:13:in `up'
/home/user/ruby_projects/ruby_auth/authapp/bin/rails:5:in `<top (required)>'
/home/user/ruby_projects/ruby_auth/authapp/bin/spring:10:in `block in <top (required)>'
/home/user/ruby_projects/ruby_auth/authapp/bin/spring:7:in `<top (required)>'
Caused by:
Mysql2::Error: 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 'inet' at line 1
/home/user/ruby_projects/ruby_auth/authapp/db/migrate/20210817054855_add_trackable_to_devise.rb:13:in `up'
/home/user/ruby_projects/ruby_auth/authapp/bin/rails:5:in `<top (required)>'
/home/user/ruby_projects/ruby_auth/authapp/bin/spring:10:in `block in <top (required)>'
/home/user/ruby_projects/ruby_auth/authapp/bin/spring:7:in `<top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Ответ №1:
Измените все вхождения inet
в вашей миграции :string
, и это должно работать нормально.