PGError:Error: must be owner of relation - ruby-on-rails

I have using rails application with PostgreSql database.
My application is working fine with PostgreSql.
I want to run my migration on server, I run a command RAILS_ENV=production rake db:migrate
then I get following error:
PGError:Error: must be owner of
relation table_name
I don't understand why this error occurs?
Please suggest any solution to resolve this error.
Thanks!

You want to change something in the table, but you don't have the permissions to do so. Only the owner of the table can do so.
Use a different database role, the owners role, and you're fine.

Related

rails db:migrate fails due to duplicate table

I got some problems about my database.
I've generated a model Comment once, but this Comment migration not what I wanted, so I drop it, and succeeded to drop. Now I try to re-generate it, and keep running rails db:migrate, but something went wrong.
It shows :
"PG::DuplicateTable: ERROR: relation "comments" already exists",
I've checked my schema.rb, did not have this comment table.
My database using "psql", and Rails version is "Rails 6.0.2.2". I've been searched related problem, but seems like not what I faced.
according your description, I have also encountered this, this is what I tried and solved:
psql
rails db
\d:
drop table tablename;
hope to solve your problem
If the data is not important you could just start fresh by dropping the current database and create a new one
rake db:drop
rake db:create
rake db:migrate
If you are sure that those migrations already have been run before, then you can go and modify the schema migrations table directly where rails stores all migrations that have been run already.
First log into your postgres and choose the correct database
sudo -u postgres psql
\c db_name
then to view the current migrations in the schema table
select * from schema_migrations;
that will show you the current migrations that rails considers as done.
Then check you migrations folder, get the version of all the migration files you want to skip running, the file name starts with the version
for example
20200401212538_add_country_name.rb
the first numeric 20200401212538 part is the version
then insert them into the migrations table
insert into schema_migrations VALUES ('version_of_migration');
Or you could also delete migrations so you could re-run them if you want to.
After that running rake db:migrate should work just fine without trying to recreate the tables.
Another solutions you might consider is that in your migrations you check first if table exists or not before trying to create it.

Location of postgresql database in nitrous.io project

I'm not sure if I'm going mad or if Heroku's dataclips are acting up but I have the following problem.
I've set up a ruby on rails development environment on Nitrous.io and have connected to a database on Heroku. In Nitrous.io I then type psqlto access postgresql.
From there I type \lto list the databases.
Then \c <name of development database hosted on heroku>
Then \dt to list the relations, one of which is users
Finally, I input the command SELECT * FROM users; and this returns relevant information on users such as email address, encrypted password etc etc.
However, when I go to Heroku and create a dataclip with the code select * from usersto be run against the same database, I'm getting the following warning
Error: Dataclip cannot be created
ERROR: relation "users" does not exist
LINE 2: select * from users
Am I completely missing something here or is the dataclip throwing an error for no reason? I'm using Devise (for the first time) in my app. Would this have anything to do with it?
Edit
I'm starting to think I've stored the database on Nitrous.io and not in Heroku. I think I may have used autoparts to install postgresql on my nitrous.io vm
I've run the command SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' as a dataclip on Heroku and the users table just isn't there. I then ran it on some other projects I have on Heroku and I can clearly see the tables I've created.
Can anyone confirm my suspicion or know how I can check the location of the database?
Yes I had indeed set the postgresql database up in nitrous.io itself rather than on Heroku. This page explains it: http://blog.nitrous.io/2013/07/02/building-a-rails-4.0-app-on-nitrous-io.html

Schema.rb doesn't include table that exists

I've run all of my migrations and my schema.rb does not include a "create table" line for table that clearly should exist. The table actually shows up in the rails console too, when I access it via its corresponding ActiveRecord class.
Any ideas why this might be happening? And what the consequences of this discrepancy in the schema could have moving forward? The project appears to be working fine.
You can update your schema.rb to mirror the database with this command.
bundle exec rake db:schema:dump
Schema.rb is not used in development or production so it has no effect on a running application. However it is used when setting up the test database. Can read about it more http://guides.rubyonrails.org/migrations.html#schema-dumping-and-you

could not find table: addresses

I am new to Ruby on Rails. I am trying to add rails_admin to my site. However, when I go to localhost:3000/admin, I keep getting this error when attempting to create the first admin account:
could not find table 'addresses'
I have tried destroying rails_admin and reinstalling, but with no success.
Any help / advice for this would be great!
This problem usually means that you have a model Address but you don't have the table created in the database.
If you have the migration (you probably do if you used a generator to create the model) you just have to run bundle exec rake db:migrate in your project's folder.
Be sure that you followed the installation instructions in https://github.com/sferik/rails_admin#installation
There is no table named "Address" in your database. If you have created this, then run the migration, using rake db:migrate

Why is rake throwing this Rails migration error?

I have two machines... a development machine and a production machine. When I first brought my rails app onto the production server, I had no problem. I simply imported schema.rb by running rake db:schema:load RAILS_ENV=production. All was well.
So, then on my development machine, I made some more changes and another migration, and then copy the new application over to the production machine. I then tried to update the database by running rake db:migrate RAILS_ENV=production. I get the following error:
"There is already an object named 'schema_migrations' in the database."
I'm thinking to myself, ya no kidding Rake... you created it! I ran trace on rake and it seems as if rake thinks it's the first time it's ever ran. However, by analyzing my 'schema_migrations' table on my development machine and my production machine you can see that there is a difference of one migration, namely the one that I want to migrate.
I have also tried to explicitly define the version number, but that doesn't work either.
Any ideas on how I can bring my production server up to date?
Update:
Let me start off by saying that I can't just 'drop' the database. It's a production server with a little over 100k records already in it. What happens if a similar problem occurs in the future? Am, I to just drop the table every time a database problem occurs? It might work this time, but it doesn't seem like a practical long term solution to every database problem. I doubt the problem I'm having now is unique to me.
It sounds like the 'schema_info' table and the 'schema_migrations' table are the same. In my setup, I only have 'schema_migrations'. As stated previously, the difference between the 'schema_migrations' table on the production server and the development machine is just one record. That is, the record containing the version number of the change I want to migrate.
From the book I read, 'Simply Rails 2', it states that when first moving to a production server, instead of running rake db:migrate, one should just run rake:db:schema:load.
If it matters, I'm using Rails version 2.1.
This is a guess, I admit: I think that because you first ran db:schema:load instead of db:migrate in your production environment, you got the structure of your db, but not the data that migrate populates into your schema_info table. So now, when you run migrate in the production environment, there is no data in schema_info which is why migrate believes that it hasn't run yet (because it hasn't).
That said... you say that you have looked in the "schema_migrations" table, and that there is a difference of one version from dev to production... I haven't heard of that table, although I'm a few months behind on my rails version. Maybe you could try creating a "schema_info" table in the production environment, with a single "version" column, and add a row with the version that you believe your production environment to be on.
If you get "There is already an object named 'schema_migrations' in the database." error message then I suspect that you are using MS SQLServer as your database? (As this seems like MS SQL Server error message)
If yes then which ActiveRecord database adapter you are using? (What is your database.yml file, what gems have you installed to access MS SQL Server database?)
Currently it seems that Rails does not find schema_migrations table in production schema and therefore tries to create it and this creation fails with database error message. Probably the reason is upper/lower case characters in schema_migrations table name - as far as I understand MS SQL Server identifiers are case sensitive.
Depending on the system used in production, I have seen instances where the below does not work:
rake db:migrate RAILS_ENV=production
But where this one does work:
RAILS_ENV=production rake db:migrate
Quirky, I know, but it's worth trying it to see if it makes a difference.
Regarding your update:
I don't understand what the difference is between your production schema_migrations and the dev version. Is there a record in both tables (there should be just 1 column, "version", right) or is there a single record in the dev DB and zero records in production? If there are zero records in the production table, then do this:
ActiveRecord::Base.connection.execute("INSERT schema_migrations (version) VALUES(#{my version number that production is supposedly on})")
Alternatively, you could try dropping the schema_migrations table totally on production:
ActiveRecord::Base.connection.execute("DROP TABLE schema_migrations")
Then, re-running rake db:migrate RAILS_ENV=production. That will run migrations from starting from version 1 though, which is probably not what you're after.
Alternatively alternatively, you could start an IRB session in your production environment, do either a "require" or "load" (I can never remember which, or if it matters) of the migration file that you want to load, and then call MyMigrationClass.up. You would need to manually set the version number in the schema_migrations table after that, as you would still have the problem going forward, but as a quick-fix type of hack, that would work.
I would just drop the DB, add it again and run rake rb:migrate. Brad is correct that when you ran the schema load, it didn't put any records in the schema_migrations table.
This is more complicated of course if there is data you can't lose on the production server. You could get the rake backup tasks (not sure if that is part of core or not) and then run rake db:backup:write on your production database, and then after you get the migrations up to date on production, run rake db:backup:read.
schema_info is from an old version of Rails. schema_migrations is the new kid on the block. You should be able to remove the schema_info table as it'll no longer be used. You'll probably want to search for any issues associated with this name change.
rake db:schema:load will load the database structure from schema.rb. This file is the current representation of the database structure. It's used when you have an empty schema (database) that needs all the tables and indexes creating. It saves you having to run all the migrations. If you have an existing production database with data in, you don't want to run it. As others have said that would be bad!
I know this post was some time ago, but I stumbled across it and it hasn't really been answered. As it comes up on google, here goes.
When you did a rake db:schema:dump (or when this was done for you by the build scripts) it will have put the definition of the migrations table into the schema.rb. At the end of the script, the process will try to create the table again, however it obviously exists already. Just remove the migrations table from the schema.rb before running rake:schema:load and there will be no error message.
You will need to set the version number in the migrations table to subsequently run migrations. So it is important to know what version your schema.rb relates too, or delete all the old migrations (they're safely in your SCM right?)
rake db:migrate RAILS_ENV=production
Use the db:schema:load task just for the first creation, incremental changes should be migrated.

Resources