gpt4 book ai didi

ruby-on-rails - 销毁后从重新创建用户模型创建数据库时出错

转载 作者:行者123 更新时间:2023-12-03 18:54:50 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Removing a model in rails (reverse of "rails g model Title...")

(5 个回答)


8年前关闭。




通过 $ rails destroy controller user_controller 销毁旧的用户模型后,我创建了另一个用户模型和 $ rails destroy model user .

我在命令行中输入了以下内容以创建新的用户模型:

$rails generate controller Users new --no-test-framework

$ rails generate model User name:string email:string

但是,当我运行 $ bundle exec rake db:migrate 时出现错误:
==  CreateUsers: migrating ====================================================
-- create_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "users" already exists: CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "email" varchar(255), "created_at" datetime, "updated_at" datetime) /usr/local/rvm/gems/ruby-1.9.3-p392/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `initialize'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `new'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `prepare'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:134:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:331:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:435:in `block in log'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-4.0.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:430:in `log'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:331:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:190:in `create_table'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:625:in `block in method_missing'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `block in say_with_time'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `say_with_time'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:617:in `method_missing'
/Users/lydia/Documents/Ruby/rails_projects/first_app/db/migrate/20131216225255_create_users.rb:3:in `change'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:571:in `exec_migration'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:555:in `block (2 levels) in migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:554:in `block in migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:553:in `migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:709:in `migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `block in ddl_transaction'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `ddl_transaction'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:920:in `block in migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:764:in `up'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/migration.rb:742:in `migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.2/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate

根据 Michael Hartl 的书,第一次运行 db:migrate 时,它​​会创建一个名为 db/development.sqlite3 的文件,这是一个 SQLite5 数据库。

已安装的 gem :
  * actionmailer (4.0.2)
* actionpack (4.0.2)
* activemodel (4.0.2)
* activerecord (4.0.2)
* activerecord-deprecated_finders (1.0.3)
* activesupport (4.0.2)
* arel (4.0.1)
* atomic (1.1.14)
* bootstrap-sass (2.3.2.0)
* builder (3.1.4)
* bundler (1.3.5)
* capybara (2.1.0)
* childprocess (0.3.9)
* coffee-rails (4.0.1)
* coffee-script (2.2.0)
* coffee-script-source (1.6.3)
* diff-lcs (1.2.5)
* erubis (2.7.0)
* execjs (2.0.2)
* ffi (1.9.3)
* hike (1.2.3)
* i18n (0.6.9)
* jbuilder (1.0.2)
* jquery-rails (3.0.4)
* json (1.8.1)
* mail (2.5.4)
* mime-types (1.25.1)
* mini_portile (0.5.2)
* minitest (4.7.5)
* multi_json (1.8.2)
* nokogiri (1.6.0)
* polyglot (0.3.3)
* rack (1.5.2)
* rack-test (0.6.2)
* rails (4.0.2)
* railties (4.0.2)
* rake (10.1.0)
* rdoc (3.12.2)
* rspec-core (2.13.1)
* rspec-expectations (2.13.0)
* rspec-mocks (2.13.1)
* rspec-rails (2.13.1)
* rubyzip (0.9.9)
* sass (3.2.12)
* sass-rails (4.0.1)
* sdoc (0.3.20)
* selenium-webdriver (2.35.1)
* sprockets (2.10.1)
* sprockets-rails (2.0.1)
* sqlite3 (1.3.8)
* thor (0.18.1)
* thread_safe (0.1.3)
* tilt (1.4.1)
* treetop (1.4.15)
* turbolinks (1.1.1)
* tzinfo (0.3.38)
* uglifier (2.1.1)
* websocket (1.0.7)
* xpath (2.0.0)

我不知道该怎么办。尽管我已经安装了 sqlite 并创建了几个模型,但它从未生成名为 db/development.sqlite3 的文件。

最佳答案

用户的表仍然存在于您的数据库中。您需要创建迁移以将其删除或手动将其从数据库中删除。

要进行迁移,您只需使用 rails g migration RemoveUsers然后 drop_table :users .如果您很久以前创建了 users 表并且有其他迁移会改变表,那么您只会想要使用迁移。如果您临时或仅在本地创建用户模型,那么您应该可以手动删除表。

关于ruby-on-rails - 销毁后从重新创建用户模型创建数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20622752/

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