为什么我不能将Rails应用程序切换到pg?

时间:2019-12-02 21:26:22

标签: ruby-on-rails ruby postgresql

我有一个可以在sqlite上运行的Rails应用程序。 但是我需要使用PostgreSQL。 我设置了pg gem,创建了用户,并在我的gemfile中更改了pg的sqlite。 然后我做Rails db:setup,然后出现错误:

Database 'BlogOnRails_development' already exists
Database 'BlogOnRails_test' already exists
rails aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "authors" does not exist

create_authors迁移:

class CreateAuthors < ActiveRecord::Migration[6.0]
  def change
    create_table :authors do |t|
      t.string :first_name
      t.string :last_name
      t.string :gender
      t.date :birthday

      t.timestamps
    end
  end
end

日志:

  [1m[35m (404.5ms)[0m  [1m[35mCREATE DATABASE "BlogOnRails_development" ENCODING = 'unicode'[0m
  [1m[35m (330.1ms)[0m  [1m[35mCREATE DATABASE "BlogOnRails_test" ENCODING = 'unicode'[0m
  [1m[35m (0.7ms)[0m  [1m[35mDROP TABLE IF EXISTS "active_storage_attachments" CASCADE[0m
  [1m[35m (23.8ms)[0m  [1m[35mCREATE TABLE "active_storage_attachments" ("id" bigserial primary key, "name" character varying NOT NULL, "record_type" character varying NOT NULL, "record_id" integer NOT NULL, "blob_id" integer NOT NULL, "created_at" timestamp NOT NULL)[0m
  [1m[35m (13.8ms)[0m  [1m[35mCREATE  INDEX  "index_active_storage_attachments_on_blob_id" ON "active_storage_attachments"  ("blob_id")[0m
  [1m[35m (8.7ms)[0m  [1m[35mCREATE UNIQUE INDEX  "index_active_storage_attachments_uniqueness" ON "active_storage_attachments"  ("record_type", "record_id", "name", "blob_id")[0m
  [1m[35m (0.6ms)[0m  [1m[35mDROP TABLE IF EXISTS "active_storage_blobs" CASCADE[0m
  [1m[35m (11.9ms)[0m  [1m[35mCREATE TABLE "active_storage_blobs" ("id" bigserial primary key, "key" character varying NOT NULL, "filename" character varying NOT NULL, "content_type" character varying, "metadata" text, "byte_size" bigint NOT NULL, "checksum" character varying NOT NULL, "created_at" timestamp NOT NULL)[0m
  [1m[35m (8.3ms)[0m  [1m[35mCREATE UNIQUE INDEX  "index_active_storage_blobs_on_key" ON "active_storage_blobs"  ("key")[0m
  [1m[35m (0.4ms)[0m  [1m[35mDROP TABLE IF EXISTS "comments" CASCADE[0m
  [1m[35m (11.3ms)[0m  [1m[35mCREATE TABLE "comments" ("id" bigserial primary key, "username" character varying, "body" text, "post_id" integer NOT NULL, "created_at" timestamp(6) NOT NULL, "updated_at" timestamp(6) NOT NULL, "author_id" integer, "ancestry" character varying, "ancestry_depth" integer DEFAULT 0)[0m
  [1m[35m (14.1ms)[0m  [1m[35mCREATE  INDEX  "index_comments_on_ancestry" ON "comments"  ("ancestry")[0m
  [1m[35m (2.8ms)[0m  [1m[35mCREATE  INDEX  "index_comments_on_author_id" ON "comments"  ("author_id")[0m
  [1m[35m (8.2ms)[0m  [1m[35mCREATE  INDEX  "index_comments_on_post_id" ON "comments"  ("post_id")[0m
  [1m[35m (0.5ms)[0m  [1m[35mDROP TABLE IF EXISTS "impressions" CASCADE[0m
  [1m[35m (17.9ms)[0m  [1m[35mCREATE TABLE "impressions" ("id" bigserial primary key, "impressionable_type" character varying, "impressionable_id" integer, "user_id" integer, "controller_name" character varying, "action_name" character varying, "view_name" character varying, "request_hash" character varying, "ip_address" character varying, "session_hash" character varying, "message" text, "referrer" text, "params" text, "created_at" timestamp(6) NOT NULL, "updated_at" timestamp(6) NOT NULL)[0m
  [1m[35m (12.8ms)[0m  [1m[35mCREATE  INDEX  "controlleraction_ip_index" ON "impressions"  ("controller_name", "action_name", "ip_address")[0m
  [1m[35m (13.7ms)[0m  [1m[35mCREATE  INDEX  "controlleraction_request_index" ON "impressions"  ("controller_name", "action_name", "request_hash")[0m
  [1m[35m (2.6ms)[0m  [1m[35mCREATE  INDEX  "controlleraction_session_index" ON "impressions"  ("controller_name", "action_name", "session_hash")[0m
  [1m[35m (7.6ms)[0m  [1m[35mCREATE  INDEX  "poly_ip_index" ON "impressions"  ("impressionable_type", "impressionable_id", "ip_address")[0m
  [1m[35m (14.3ms)[0m  [1m[35mCREATE  INDEX  "poly_params_request_index" ON "impressions"  ("impressionable_type", "impressionable_id", "params")[0m
  [1m[35m (2.7ms)[0m  [1m[35mCREATE  INDEX  "poly_request_index" ON "impressions"  ("impressionable_type", "impressionable_id", "request_hash")[0m
  [1m[35m (8.2ms)[0m  [1m[35mCREATE  INDEX  "poly_session_index" ON "impressions"  ("impressionable_type", "impressionable_id", "session_hash")[0m
  [1m[35m (14.1ms)[0m  [1m[35mCREATE  INDEX  "impressionable_type_message_index" ON "impressions"  ("impressionable_type", "message", "impressionable_id")[0m
  [1m[35m (2.2ms)[0m  [1m[35mCREATE  INDEX  "index_impressions_on_user_id" ON "impressions"  ("user_id")[0m
  [1m[35m (0.5ms)[0m  [1m[35mDROP TABLE IF EXISTS "posts" CASCADE[0m
  [1m[35m (23.0ms)[0m  [1m[35mCREATE TABLE "posts" ("id" bigserial primary key, "name" character varying, "title" character varying, "content" text, "created_at" timestamp(6) NOT NULL, "updated_at" timestamp(6) NOT NULL, "author_id" integer, "impressions_count" integer)[0m
  [1m[35m (2.9ms)[0m  [1m[35mCREATE  INDEX  "index_posts_on_author_id" ON "posts"  ("author_id")[0m
  [1m[35m (0.4ms)[0m  [1m[35mDROP TABLE IF EXISTS "votes" CASCADE[0m
  [1m[35m (9.5ms)[0m  [1m[35mCREATE TABLE "votes" ("id" bigserial primary key, "comment_id" integer NOT NULL, "author_id" integer NOT NULL, "created_at" timestamp(6) NOT NULL, "updated_at" timestamp(6) NOT NULL, "votes" integer DEFAULT 0)[0m
  [1m[35m (13.2ms)[0m  [1m[35mCREATE  INDEX  "index_votes_on_author_id" ON "votes"  ("author_id")[0m
  [1m[35m (2.2ms)[0m  [1m[35mCREATE  INDEX  "index_votes_on_comment_id" ON "votes"  ("comment_id")[0m
  [1m[35m (3.4ms)[0m  [1m[35mALTER TABLE "active_storage_attachments" ADD CONSTRAINT "fk_rails_c3b3935057"
FOREIGN KEY ("blob_id")
  REFERENCES "active_storage_blobs" ("id")
[0m
  [1m[35m (2.3ms)[0m  [1m[35mALTER TABLE "comments" ADD CONSTRAINT "fk_rails_2fd19c0db7"
FOREIGN KEY ("post_id")
  REFERENCES "posts" ("id")
[0m
  [1m[35m (1.3ms)[0m  [1m[35mALTER TABLE "votes" ADD CONSTRAINT "fk_rails_96086a30ba"
FOREIGN KEY ("author_id")
  REFERENCES "authors" ("id")
[0m

当我做rails db:重置时,我得到同样的错误。

1 个答案:

答案 0 :(得分:2)

还必须在config / database.yml文件中更改适配器。

但是为了使事情变得更容易,从Rails 6开始,您可以使用任务df['listings_per_zip'] = df.groupby(level=[0,1]).sum().groupby(level=[1]).cumsum()

db:system:change