質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Git

Gitはオープンソースの分散バージョン管理システム(DVCS)です。

Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Q&A

解決済

1回答

1382閲覧

herokuでDBの情報を反映させたい

ren0826nosuke

総合スコア34

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Git

Gitはオープンソースの分散バージョン管理システム(DVCS)です。

Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

1グッド

1クリップ

投稿2020/04/16 13:01

引用テキスト# 解決したいこと

herokuにDBの情報を反映させたい

環境

rails 5.2.3
ruby 2.5.1

状況

現在、アプリが完成しherokuでデプロイしようとしました。
そこで下記手順で行いましたがデプロイは完了していますがDB情報が反映されておらず正常にページが作動していませんでした。「target_category」がない的なエラーが出ていると思いますが、「target_category」というカラムは名前変更しているため「category_id」になっており今はありません。
どのようにすれば正常に作動しますか?

手順

1.heroku login

2.Gemfileを以下のように編集する
gem 'sqlite3' をgroup :development do ~ end の中に入れる。

group :production do ~end の作成し中に、gem 'pg' を入れる。

3.bundle --without production

4.git init

5.heroku create アプリ名

6.git add *

7.git commit -m “initial commit”

8.git push heroku master

9.heroku run rails db:migrate

10.heroku run rails db:seed

11.heroku open

コード記述

Gemfile

ruby

1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby '2.5.1' 5 6# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 7gem 'rails', '~> 5.2.3' 8# Use sqlite3 as the database for Active Record 9# Use Puma as the app server 10gem 'puma', '~> 3.11' 11# Use SCSS for stylesheets 12gem 'sass-rails', '~> 5.0' 13# Use Uglifier as compressor for JavaScript assets 14gem 'uglifier', '>= 1.3.0' 15# See https://github.com/rails/execjs#readme for more supported runtimes 16# gem 'mini_racer', platforms: :ruby 17 18# Use CoffeeScript for .coffee assets and views 19gem 'coffee-rails', '~> 4.2' 20# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 21gem 'turbolinks', '~> 5' 22# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 23gem 'jbuilder', '~> 2.5' 24# Use Redis adapter to run Action Cable in production 25# gem 'redis', '~> 4.0' 26# Use ActiveModel has_secure_password 27# gem 'bcrypt', '~> 3.1.7' 28 29# Use ActiveStorage variant 30# gem 'mini_magick', '~> 4.8' 31 32# Use Capistrano for deployment 33# gem 'capistrano-rails', group: :development 34 35# Reduces boot times through caching; required in config/boot.rb 36gem 'bootsnap', '>= 1.1.0', require: false 37 38group :development, :test do 39 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 40 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 41 gem 'rspec-rails' 42 gem 'factory_bot_rails' 43end 44 45group :production do 46 gem 'pg' 47end 48 49group :development do 50 gem 'sqlite3' 51 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 52 gem 'web-console', '>= 3.3.0' 53 gem 'listen', '>= 3.0.5', '< 3.2' 54 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 55 gem 'spring' 56 gem 'spring-watcher-listen', '~> 2.0.0' 57end 58 59group :test do 60 # Adds support for Capybara system testing and selenium driver 61 gem 'capybara', '>= 2.15' 62 gem 'selenium-webdriver' 63 # Easy installation and use of chromedriver to run system tests with Chrome 64 gem 'chromedriver-helper' 65end 66 67# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 68gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 69 70gem "sass-rails","~>5.0" 71gem "bootstrap-sass","~>3.3.6" 72gem "jquery-rails" 73gem "jquery-ui-rails" 74 75gem "font-awesome-sass" 76 77gem 'autonumeric-rails' 78 79gem 'pry-rails' 80

マイグレーションファイルは以下の通りです。

ruby

1class CreateTargets < ActiveRecord::Migration[5.2] 2 def change 3 create_table :targets do |t| 4 t.string "title" 5 t.text "memo" 6 t.date "date" 7 t.integer "targets_category" 8 t.integer "priority_category" 9 t.timestamps 10 end 11 end 12end 13

ruby

1class CreateCategories < ActiveRecord::Migration[5.2] 2 def change 3 create_table :categories do |t| 4 t.string "name" 5 t.timestamps 6 end 7 end 8end 9

ruby

1class AddPriceToTargets < ActiveRecord::Migration[5.2] 2 def change 3 add_column :targets, :price, :integer 4 end 5end 6

ruby

1class RenameTargetsCategoryColumnToTargets < ActiveRecord::Migration[5.2] 2 def change 3 rename_column :targets, :targets_category, :category_id 4 end 5end 6

ruby

1class RenamePriorityCategoryColumnToTargets < ActiveRecord::Migration[5.2] 2 def change 3 rename_column :targets, :priority_category, :priority 4 end 5end 6

ruby

1class RenamePriorityCategoriesToPriorities < ActiveRecord::Migration[5.2] 2 def change 3 rename_table :priority_categories, :priorities 4 end 5end 6

ruby

1class RenamePriorityColumnToTargets < ActiveRecord::Migration[5.2] 2 def change 3 rename_column :targets, :priority, :priority_id 4 end 5end 6

ruby

1class AddPositionToTargets < ActiveRecord::Migration[5.2] 2 def change 3 add_column :targets, :position, :integer 4 end 5end 6

seeds.rb

ruby

1Category.find_or_create_by(name:"旅行") 2Category.find_or_create_by(name:"なりたい自分") 3Category.find_or_create_by(name:"アクティビティ") 4Category.find_or_create_by(name:"仕事") 5Category.find_or_create_by(name:"健康美容") 6Category.find_or_create_by(name:"ライフスタイル") 7Category.find_or_create_by(name:"家族") 8Category.find_or_create_by(name:"恋人") 9Category.find_or_create_by(name:"欲しいもの") 10Priority.find_or_create_by(name:"★") 11Priority.find_or_create_by(name:"★★") 12Priority.find_or_create_by(name:"★★★") 13Priority.find_or_create_by(name:"★★★★") 14Priority.find_or_create_by(name:"★★★★★") 15 16

エラー文

※文字超過のためエラー文を抜粋してます。
必要であれば追記します。

ruby

1Running rails db:migrate on ⬢ ren-target-app-0416... up, run.7460 (Free) 2D, [2020-04-16T12:54:01.410763 #4] DEBUG -- : (0.9ms) SELECT pg_try_advisory_lock(3101651796077205420) 3D, [2020-04-16T12:54:01.427252 #4] DEBUG -- : (1.7ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC 4I, [2020-04-16T12:54:01.428938 #4] INFO -- : Migrating to RenameTargetsCategoryColumnToTargets (20200414031534) 5D, [2020-04-16T12:54:01.432758 #4] DEBUG -- : (0.8ms) BEGIN 6== 20200414031534 RenameTargetsCategoryColumnToTargets: migrating ============= 7-- rename_column(:targets, :targets_category, :category_id) 8D, [2020-04-16T12:54:01.434678 #4] DEBUG -- : (1.5ms) ALTER TABLE "targets" RENAME COLUMN "targets_category" TO "category_id" 9D, [2020-04-16T12:54:01.435645 #4] DEBUG -- : (0.8ms) ROLLBACK 10D, [2020-04-16T12:54:01.436821 #4] DEBUG -- : (0.9ms) SELECT pg_advisory_unlock(3101651796077205420) 11rails aborted! 12StandardError: An error has occurred, this and all later migrations canceled: 13 14PG::UndefinedColumn: ERROR: column "targets_category" does not exist 15: ALTER TABLE "targets" RENAME COLUMN "targets_category" TO "category_id" 16(中略) 17Caused by: 18ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "targets_category" does not exist 19: ALTER TABLE "targets" RENAME COLUMN "targets_category" TO "category_id" 20(中略) 21Caused by: 22PG::UndefinedColumn: ERROR: column "targets_category" does not exist 23(中略) 24Tasks: TOP => db:migrate 25(See full trace by running task with --trace)
DrqYuto👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

カラム名を適切な書き方で設定し、テーブル作成したらどうなりますか?

例:t.string "title"t.string :title

class CreateTargets < ActiveRecord::Migration[5.2] def change create_table :targets do |t| t.string :title t.text :memo t.date :date t.integer :targets_category t.integer :priority_category t.timestamps end end end class CreateCategories < ActiveRecord::Migration[5.2] def change create_table :categories do |t| t.string :name t.timestamps end end end

https://railsguides.jp/active_record_migrations.html

投稿2020/04/17 00:06

no1knows

総合スコア3365

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問