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
Related
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.
I'm trying to remove an old model etc., that I had created by scaffolding several migrations past. So I followed the advice in this thread and ran a drop_table migration and the rails d scaffold Modelname command (maybe I did that first, not sure anymore).
As far as I can tell that removed the table and all the other files that had been created by the scaffolding, but my issue is that the schema.rb still includes the table! I've read here that I could probably fix this issue by running rails db:drop db:create db:migrate, but I was wondering if there is a way to fix schema.rb without resetting the database completely? (Unfortunately I started populating my db by console while I was adding parts to my app instead of writing a proper seed file.)
you can drop the table from rails console, for example the model is user then your table will be users
$rails console
Then just type:
ActiveRecord::Migration.drop_table(:users)
Additional answer for user problem:
to update table schema in schema.rb you can use
$ rake db:schema:dump
I have a simple Rails app which populates into a certain database and there are few migration scripts. I would like to create a different set of model classes and actually migrate the schema pertaining to those files into a different database. How should we do that? Will I be able to do this from the same rails app?
Well, change the database name in database.yml , then follow the usual steps,
bundle exec rake db:setup should do the work.
Or you could create a seperate database_2.yml and then make a database.rake file and follow the steps here
When I read Rails book, each time they create a new database, always follow a db:migrate.
rails generate scaffold school
rake db:migrate
In console view, I see at first line, Rails create some files, no problem. but in second line, I see that Rails isn't really change anything. I have view some files that Rails nearly create and see no change too.
So, what the purpose of line 2, please tell me.
Thanks :)
The rake migrates the changes into your database. It is which acttually changes the database schema to match your previously generated scaffolded model.
Without it, you wouldn't have a table to write your objects into. Or in case of changed model, the table could differ from your model, leading to error.
When you generate a model (or scaffold one) a migration file is created in your db/migration directory. It is a pure text file, you can create such manually, if you want. This is the tool for the iterative development in rails regarding the database. Each migration adds some change to the system. When you run rake db:migrate your database is updated by the given migrations. This is a handy tool in case of distributed development, when one programmer can check out the code from the repository, and can run the migrations on his own development database.
db:migrate, is the command that tells rails to update the database with new changes. Think of it as this way
when u say rails generate scaffold rails will generate files like a model, controller etc.. and it create a file under db/migrate which has the sql script to update the database.
Ex: if you run rails generate scaffold User name:string, then you will need a table called users in the database with the column 'name', that sql script will generated under db/migrate folder
with db:migrate, command, you are telling rails to migrate new sql scripts to the database, in the above case, it will creates the 'users' table
if you run rake -T, from your rails application root, you could see all the rake tasks
HTH :)
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.