heroku run rails db:seed failed - Rails 7 + fixtures - ruby-on-rails

I have successfully deployed my Rails app with Heroku, heroku run rails db:migrate, although heroku run rails db:seed failed.
Here is the error message
➜ quote-editor git:(master) heroku run rails db:seed
Running rails db:seed on ⬢ awesome-quote-editor... up, run.6832 (Free)
== Seeding the database with fixtures ==
WARNING: Rails was not able to disable referential integrity.
This is most likely caused due to missing permissions.
Rails needs superuser privileges to disable referential integrity.
cause: PG::InsufficientPrivilege: ERROR: permission denied: "RI_ConstraintTrigger_c_783704777" is a system trigger
rails aborted!
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "line_item_dates" violates foreign key constraint "fk_rails_048689f618"
DETAIL: Key (quote_id)=(309456473) is not present in table "quotes".
My setup
Ubuntu 20.04.2
Ruby 3.1.2
Rails 7.0.4
pg 1.4.3 with native extensions
I have both tried to give superuser privileges
ALTER USER myuser WITH SUPERUSER;
and
GRANT ALL PRIVILEGES ON DATABASE quote_editor_production TO myuser;
List of databases
Name | Owner | Encoding | Collate | Ctype | Access
----------------------------- | -------- | -------- | ------- | ------- | ------------
quote_editor_development | paulinetw | UTF8 | C.UTF-8 | C.UTF-8 |
----------------------------- | -------- | -------- | ------- | ------- | ------------
quote_editor_production | paulinetw | UTF8 | C.UTF-8 | C.UTF-8 | =Tc/paulinetw
quote_editor_test | paulinetw | UTF8 | C.UTF-8 | C.UTF-8 |
database.yml
default: &default
adapter: postgresql
encoding: unicode
development:
<<: *default
database: quote_editor_development
test:
<<: *default
database: quote_editor_test
production:
<<: *default
database: quote_editor_production
username: quote_editor
password: <%= ENV["QUOTE_EDITOR_DATABASE_PASSWORD"] %>
I have tried a lot of suggestions but still can't find a way out. I will really appreciate if somebody could help.

ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "line_item_dates" violates foreign key constraint "fk_rails_048689f618" DETAIL: Key (quote_id)=(309456473) is not present in table "quotes".
Your seed file is trying to use a Quote with ID 309456473, but that quote doesn't exist.
UPDATE:
Here are my assumptions:
In producution, you run heroku run rails db:migrate, which works
In production, you run heroku run rails db:seed, which fails.
In your seed file you have some command that relies upon some code similar to Quote.find(309456473) and this fails in production
In your seed file, when you create Quotes, you are manually setting each quote's ID as you create it.
Setting the ID of your records on creation is not a good idea. Let the Db generate keys.
In your seed file, wherever you are looking up records by ID, find a different method to locate the record you want by some other attribute.
Don't assume that Heroku Postgres respects or preserves your record IDs from the seed file.

Related

Rails error when firing up a cloned API - ActiveRecord::ConnectionNotEstablished

As the title says, I clones a rails API. I tried to follow the steps in this article from point 2 onwards https://dev.to/w3ndo/a-checklist-for-setting-up-a-cloned-rails-application-locally-5468 but I keep getting the same error from db:setup onwards.
Please help!
I have tried googling the answer and phoning a friend.
I have tried rails db:setup, rails db:seed, rails db:create, rails db:migrate.
Update: So I found I was getting this error because the db owner was listed as the original owner in the repo but when I typed psql in terminal and located the db, the owner was listed as me.
I was able to change this using PGadmin 4 and type in the original owner as the db owner.
You want to initialize the postgres db, which doesn't quite come for free. I recommend using sqlite3 until you need a production db. If the clone calls for PG, then:
(user) $ sudo su - postgres
(postgres) $ createuser --interactive
(local) $ sudo systemctl restart postgresql
(local) $ bundle exec rails db:create:all
$ pg_isready tells you at a glance if posgtres server/cluster is online.
$ pg_isready
/tmp:5432 - accepting connections
If it gets frustrating, change the config/database.yml to the default version, remove pg gem if possible, add sqlite3. Then simple rake db:migrate after creating or adding an [environment].sqlite3 file to db/
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
Update
Thank you for the help. I was able to find out as my partner had created the backend, the issue was arising because the owners of the database didn't match when I cloned his repo.
So I found I was getting this error because the db owner was listed as the original owner in the repo but when I typed psql in terminal and located the db, the owner was listed as me.
I was able to change this using PGadmin 4 and type in the original owner as the db owner.

capistrano, rails, PG::ConnectionBad: fe_sendauth: no password supplied

I'm trying to run Capistrano for a rails app with Postgres on Ubuntu 14, I ran into a password error during rake db:migrate-
DEBUG [2823f146] Command: cd /home/ben/apps/mll/releases/20160414014303 && ( export RBENV_ROOT="$HOME/.rbenv" RBENV_VERSION="2.0.0-p645" RAILS_ENV="production" ; $HOME/.rbenv/bin/rbenv exec bundle exec rake db:migrate )
DEBUG [2823f146] rake aborted!
PG::ConnectionBad: fe_sendauth: no password supplied
I tried every solution to similar posts but no luck. For kicks I also tried running just that command in the remote app dir and got the following:
PG::ConnectionBad: FATAL: password authentication failed for user "mll"
FATAL: password authentication failed for user "mll"
This is interesting because it's using my database name as my username. See database.yml below, so for the hell of it I added a mll role, and lo and behold it worked when just running rake db:migrate. I tried running Capistrano again though with this new role, and still no luck.
Is it a reasonable guess that the username is not being accessed/stored properly? Any way for me to test that? I manually ALTER ROLE ben WITH PASSWORD 'mypw'; for both my ben and mll roles, and nothing.
My database.yml:
defaults: &default
adapter: sqlite3
encoding: utf8
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/development.sqlite3_test
production:
<<: *default
host: localhost
adapter: postgresql
encoding: utf8
database: mll
pool: 5
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
\du:
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
ben | Superuser, Create role, Create DB | {}
mll | Superuser, Create role, Create DB | {}
postgres | Superuser, Create role, Create DB, Replication | {}
I read changing md5 to trust helped some people, I tried that, but I'm unsure how to restart, all commands I've seen haven't worked for me.
pg_hba.conf:
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
You should use "trust" under method for localhost in pg_hba.conf. Note that this means all connections from localhost will be able to login as any user, which is probably fine as long as you're using this for development.
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
After changing your pg_hba.conf you can restart postgres with pg_ctl reload
The best practice to resolve the issue is -
Add the below gem in your Gemfile under development group and bundle install.
gem 'capistrano-postgresql'
Add the below line in Capfile
require 'capistrano/postgresql'
Add below lines in config/deploy.rb or in config/deploy/*.rb
set :pg_password, ENV['DATABASE_PASSWORD']
set :pg_ask_for_password, true

Creating postgres database on Heroku with Rails 4 error

PG::UndefinedTable: ERROR
I am trying to deploy a Rails 4 app on Heroku with a postgres database. I have precompiled assets, but now when I run:
heroku run rake db:migrate
I get this error:
PG::UndefinedTable: ERROR: relation "users" does not exist
: ALTER TABLE "users" ADD COLUMN "invited_by_user_id" integer
rake aborted!
Commonly suggested solutions on Stack Overflow:
I have tried resetting the database with:
heroku pg:reset
The database resets, but I still can't run db:migrate.
I have also tried:
heroku run db:setup
but I get an error that says:
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
I was using Rails 4.1.8, and saw this question which suggested maybe I needed to update to rails 4.1.9 or above, but even now that I'm using Rails 4.2.2, no luck.
Additional possibly relevant info:
Connecting to the postgres database on heroku with
heroku pg:psql
then listing the databases with '\l' gives me a long list of databases with cryptic names:
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------------+----------------+----------+-------------+-------------+-----------------------------------
d100o5a2gii5he | rdxtahlqikernc | UTF8 | en_US.UTF-8 | en_US.UTF-8 | rdxtahlqikernc=CTc/rdxtahlqikernc
d102ehvv3umqam | ibuffadisyolrb | UTF8 | en_US.UTF-8 | en_US.UTF-8 | ibuffadisyolrb=CTc/ibuffadisyolrb
d103n2kea6j5sa | tzzkfcinullytw | UTF8 | en_US.UTF-8 | en_US.UTF-8 | tzzkfcinullytw=CTc/tzzkfcinullytw
d10597iiu347nk | eltovdzaxajtiu | UTF8 | en_US.UTF-8 | en_US.UTF-8 | eltovdzaxajtiu=CTc/eltovdzaxajtiu
d108g0imdgn865 | pvzysfivyhcnaa | UTF8 | en_US.UTF-8 | en_US.UTF-8 | pvzysfivyhcnaa=CTc/pvzysfivyhcnaa
...
dvs2j8tkv153m | zjefhzzizozpwy | UTF8 | en_US.UTF-8 | en_US.UTF-8 | zjefhzzizozpwy=CTc/zjefhzzizozpwy
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres
(4787 rows)
I'm not sure exactly how to interpret that.
I am relatively new new to Rails, but have successfully setup several databases on Heroku in the past. Following the Heroku documentation hasn't helped, and I'm not sure where to go from here.
How can I get db:migrate to complete without errors?
NOTE: I solved this just after posting. See answer below.
Minutes after posting this question I figured it out, and I'm hoping this might help someone else out.
The solution was to upgrade from Rails 4.1.8 to Rails 4.2.2.
Then run
heroku run rake db:schema:load
before
heroku run rake db:migrate
For some reason that wouldn't run for my project with Rails 4.1.8. So, after upgrading to Rails 4.2.2, I ran the schema load and migrate commands with no errors.

Where is my Rails database?

I paid someone to create a program with Ruby on Rails where it scrapes data and puts it in a Postgres database. In the program's directory are the standard Rails folders, application, "bin", "config" and other directories.
I'm trying to see a list of the columns in a table. I think the best, or only way, to do this is to log into the actual database, and print it out. I'm trying to use a "psql" command to log in but it is saying:
psql: FATAL: database "dan" does not exist
I'm not sure where the database is, or how I can find it.
This is what the config/database.yml contains:
development:
adapter: postgresql
database: danwork
pool: 5
timeout: 5000
encoding: unicode
username: dan
password: supersecretpassword
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
database: sports
pool: 5
timeout: 5000
encoding: unicode
username: namename
password: sports_db
Where is my database?
How could I find the database on my own using some linux commands, like find . -iname '...'?
How do I log in, and print out all the columns for the table named "games"?
You want rails dbconsole or rails db.
From the Rails Guide:
rails dbconsole figures out which database you’re using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.
You can also use the alias “db” to invoke the dbconsole: rails db.
To see the contents of your games table:
~/Documents/workspace/<project-dir>$ rails db
psql (9.6.3)
Type "help" for help.
development=# select * from games;
id | date | description | city | state
----+------+-------------+----------------------+-------
1 | | | Boston | MA
2 | | | Seattle | WA
(2 rows)
development=# \q
~/Documents/workspace/<project-dir>$
If you really want to use psql -U <username> <dbname>, you can find these parameters as described in this answer and paraphrased here:
~/Documents/workspace/<project-dir>$ rails c
Loading development environment (Rails 4.1.4)
2.3.1 :001 > Rails.configuration.database_configuration[Rails.env]
=> {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"development"}
2.3.1 :002 > exit
~/Documents/workspace/<project-dir>$ psql development
psql (9.6.3)
Type "help" for help.
development=# \q
~/Documents/workspace/<project-dir>$
As you can see, if no username is returned, you won't need the -U flag.
Your database is running on a server independent of Rails (it will be set up by your coder). It looks like your database is on your localhost.
It will be dependent on your server OS.
If you're looking to show column names in your database, you'll be able to do this in the Rails Console:
$ rails c
$ Game.column_names
To access your database using plain PostgreSQL
su username
psql
\db database_name;
To connect with psql, you must specify the database name.
If you're using password auth, simply psql -U username dbname.
If you're using peer auth, sudo -u username psql dbname.
In your case you seem to be using password auth and connecting to the local host, so I suggest:
psql -U dan danwork
and entering the password when prompted. (You can use a ~/.pgpass file to save the password if desired; see the pgpass documentation)

Postgres drop database in rails app with rake command

I am having trouble dropping my postgres database for a rails app - switched recently from sqlite after deploying to heroku. When running my rake task to aggregate data and populate the database, rake db:create db:migrate all works, but db:drop is not working. I am getting the following error in my terminal after running rake db:drop db:migrate db:create all. 'all' is a rake task for an aggregator.
Couldn't drop something_development : #<PG::Error: FATAL: database "postgres" does not exist
>
Couldn't drop something_test : #<PG::Error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
>
something_development already exists
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
'This is a look at my database.yml file...`
development:
adapter: postgresql
host: localhost
database: something_development
username: username
test:
adapter: postgresql
database: something_test
pool: 5
timeout: 5000
production:
adapter: postgresql
host: localhost
username: username
database: something_production
Also here is a list of databases from psql command line:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------------------+-----------------+----------+---------+-------+-------------------------------------
username | username | UTF8 | en_US | en_US |
something_development | username | UTF8 | en_US | en_US |
template0 | username | UTF8 | en_US | en_US | =c/username+ | | | | | username=CTc/username
template1 | username | UTF8 | en_US | en_US | =c/username + | | | | | username=CTc/username
(4 rows)
EDIT - I am trying to do this in development before pushing to heroku. Do i need to add access privileges to my username? I tried GRANT UPDATE ON something_development TO username but received ERROR: relation "something_development" does not exist. Will remember that for Heroku - thanks.
Does anyone have any ideas on what might be happening or some resources that might point me in the right direction? Appreciate any help - thanks.
At first, you should make sure that which kind of postgres DB using on Heroku: Shared DB or Heroku Postgres.
If you are using Shared DB (it's recommended that shouldn't use any more), unfortunately you have to remove your Heroku app and re-create new one with the same app name.
If you are using Heroku PG, you could find your DB here: https://postgres.heroku.com/databases and just destroy and add another DB, it's like drop and re-create your DB, after that you can run migration as well. With Heroku PG, I'm not sure whether we can run db:drop now (It's been a while since I was fail to use this command on Heroku)
About configuration you should follow few steps below:
install gem pg
using pgAdmin III or something like that to try to connect to your PG server. If you're successful, use this information for your database.yml file.
Notice: When you push your code to Heroku, Heroku will automatically create new database.yml basing on their PG server information, so you don't need to care about configuration database on Heroku. Remember removing sqlite gem before pushing your app to Heroku
On heroku you don't have privileges for drop database, you have to do
heroku pg:reset
If your database is not on heroku, check the user (of postrgesql) has privileges.
Figured out the problem - turns out you cannot rename the database "postgres" because it needs that to manage the database. I had tried renaming postgres to something_development. To fix this, I created a postgres database.

Resources