gpt4 book ai didi

mysql - Rails 列带有 ":references"-- 困惑

转载 作者:行者123 更新时间:2023-11-29 08:42:37 25 4
gpt4 key购买 nike

我有一个用户表“belongs_to :user_category”和user_category“has_many :users”。我正在尝试将类型设置为“:references”,如下所示...

class AddUserCategoryToUsers < ActiveRecord::Migration
def change
add_column :users, :user_category, :references, :default => 1
end
end

但它似乎不起作用:

==  CreateUsers: migrating ====================================================
-- create_table(:users)
-> 0.0114s
== CreateUsers: migrated (0.0115s) ===========================================

== CreateUserCategories: migrating ===========================================
-- create_table(:user_categories)
-> 0.0118s
== CreateUserCategories: migrated (0.0119s) ==================================

== AddUserCategoryToUsers: migrating =========================================
-- add_column(:users, :user_category, :references, {:default=>1})
rake aborted!
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 'references DEFAULT 1' at line 1: ALTER TABLE `users` ADD `user_category` references DEFAULT 1

Tasks: TOP => db:migrate
(See full trace by running task with --trace)

这是完整的跟踪:

-- add_column(:users, :user_category, :references, {:default=>1})
rake aborted!
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 'references DEFAULT 1' at line 1: ALTER TABLE `users` ADD `user_category` references DEFAULT 1
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:461:in `add_column'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:466:in `block in method_missing'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:438:in `block in say_with_time'
/Users/jakebuob/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/benchmark.rb:280:in `measure'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:438:in `say_with_time'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:458:in `method_missing'
/Users/jakebuob/Sites/pears/db/migrate/20121114033751_add_user_category_to_users.rb:3:in `change'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:407:in `block (2 levels) in migrate'
/Users/jakebuob/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/benchmark.rb:280:in `measure'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:407:in `block in migrate'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:123:in `with_connection'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:389:in `migrate'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:528:in `migrate'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:777:in `call'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:777:in `ddl_transaction'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:719:in `block in migrate'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:700:in `each'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:700:in `migrate'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:570:in `up'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:551:in `migrate'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:153:in `block (2 levels) in <top (required)>'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/jakebuob/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/bin/rake:19:in `load'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/bin/rake:19:in `<main>'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/bin/ruby_noexec_wrapper:14:in `eval'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:migrate

最佳答案

:references 类型不存在。您需要创建真正的列。

class AddUserCategoryToUsers < ActiveRecord::Migration
def change
add_column :users, :user_category_id, :integer, :default => 1
end
end

创建索引也是一个好主意。

class AddUserCategoryToUsers < ActiveRecord::Migration
def change
add_column :users, :user_category_id, :integer, :default => 1
add_index :users, :user_category_id
end
end

.references 方法仅作为表定义的一部分存在 ( here's an example )

class AddUserCategoryToUsers < ActiveRecord::Migration
def change
change_table :users do |t|
t.references :user_category, :default => 1
end
end
end

关于mysql - Rails 列带有 ":references"-- 困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13381896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com