“rails db:migrate”错误:关系 “users” 不存在?

huangapple go评论93阅读模式
英文:

"rails db:migrate" error : relation "users" does not exist?

问题

我是新手使用Rails,以下是我的问题。

我创建了一个非常简单的Rails程序,在db > migrate > 2023.._add_columns_to_user.rb文件中,我将以下代码添加到该文件中

  1. class AddColumnsToUser < ActiveRecord::Migration[7.0]
  2. def change
  3. add_column :users, :full_name, :string
  4. add_column :users, :from, :string
  5. add_column :users, :about, :text
  6. add_column :users, :language, :string
  7. add_column :users, :status, :boolean
  8. add_column :users, :status, :boolean, default: false
  9. end
  10. end

然后我运行了这段代码

  1. rails db:migrate

但是它给了我这个错误

  1. rails aborted!
  2. StandardError: An error has occurred, this and all later migrations canceled:
  3. PG::UndefinedTable: ERROR: relation "users" does not exist

也许我必须先创建数据库吗?
你能否就这个问题给我一些建议?
如果需要参考,以下是我所有的代码。

https://github.com/nguyencuc2586/Addcustomfieldsoutusermodel

提前感谢你。

英文:

I am newbie with rails and here is my problem.

I created a very simple rails program and in the db > migrate > 2023.._add_columns_to_user.rb file, I added this code to this file

  1. class AddColumnsToUser < ActiveRecord::Migration[7.0]
  2. def change
  3. add_column :users, :full_name, :string
  4. add_column :users, :from, :string
  5. add_column :users, :about, :text
  6. add_column :users, :language, :string
  7. add_column :users, :status, :boolean
  8. add_column :users, :status, :boolean, default: false
  9. end
  10. end

Then I ran this code

  1. rails db:migrate

But it gave me this error

  1. rails aborted!
  2. StandardError: An error has occurred, this and all later migrations canceled:
  3. PG::UndefinedTable: ERROR: relation "users" does not exist

May be I must create the database first?
Could you please give me some advices for this problem?
Here is all of my code, if you need for reference.

  1. https://github.com/nguyencuc2586/Addcustomfieldsoutusermodel

Thank you in advance.

答案1

得分: 1

你不能修改尚未创建的表。你的迁移应该包含在一个表定义中:

  1. class CreateUsers < ActiveRecord::Migration[7.0]
  2. def change
  3. create_table :users do |t|
  4. t.string :full_name # 等同于 add_column :users, :full_name, :string
  5. t.string :from
  6. t.text :about
  7. t.boolean :status, default: false # 为什么这是一个布尔值?
  8. t.timestamps
  9. end
  10. end
  11. end

这会生成CREATE TABLE users (...),而你的迁移会生成ALTER TABLE users (...)

英文:

You can't alter a table that hasn't yet been created. Your migration should be wrapped in a table defintion:

  1. class CreateUsers < ActiveRecord::Migration[7.0]
  2. def change
  3. create_table :users do |t|
  4. t.string :full_name # shorthand for add_column :users, :full_name, :string
  5. t.string :from
  6. t.text :about
  7. t.boolean :status, default: false # why on earth is this a boolean?
  8. t.timestamps
  9. end
  10. end
  11. end

This results in CREATE TABLE users (...) while your migration would result in ALTER TABLE users (...).

huangapple
  • 本文由 发表于 2023年2月18日 19:08:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定