I've tried to solve this issue a few times now, but with no luck.
I recently switched my app over to Postgress from Sqlite3, but a now having issues generating models in Rails. The weird thing is that when I run a Rake db:migrate my terminal window states that the tables were created successfully. Although when I look into my Schema file the nearly created model doesn't show up. The model's attributes aren't accessible through rails console, which seems to let me know that the model doesn't yet exist in the database.
Am I wrong in my assumptions, or do you guys think there is something else going on here? I've googled this issue, but can't seem to find any relevant posts regarding this issue, so any help or suggestions is welcome.
Chances are the tables are created outside your search path.
Try \dn to list namespaces and then
\d myschema.*
To list all structures of tables within my schema. You can also
SET search_path = 'myschema';
\d
RESET search_path;
Related
I am new to Rails and I am facing the following problem. There were different tables in the existing project schema such as questionnaire, test, answers, questions, etc. But they did not have the columns of the creation date and the update date, and there were no models and views yet. So I decided to delete them and recreate them. Unfortunately I did it directly through the pgAdmin. Then I recreated each model with a scaffold generator. Everything was going fine until I got to the Questions model. For this model, I forgot to delete the table from the database, which I decided to do. But when I returned to the terminal and tried again to migrate the creation of the question table, I came across the same error message that the table already exists. Moreover, in the schema, I found that all tables manually removed from the database are still present.
What I tried:
I wrote migrations to drop tables. Migrations go through, tables disappear from the schema. Then I tried to write again table creation migrations, they are also started and tables are added to the schema, but they are not created in the database itself. But the application is still accessing the database, retrieving data, inserting it (into other tables that I have not touched on).
How can I return everything to the original? Migrations do not work in the project and I do not understand how to fix it. And I would not really like to dump the database, since it contains a lot of data.
Also in the project there are old migrations of test creation, test results, etc., which I deleted manually through the PG Admin. Should I delete them?
db/migrate
I found a bug. The problem was that the project turns out to be using docker (did not fully understand what it is). But as I understand it, for any update of the project, it must be reloaded through 4 commands in the terminal. Therefore, migrations did not go through the terminal. they had to be done by the docker himself. I expected to see the error logs right in the terminal, and they were issued by docker. I should have connected to the docker logs and read errors from there. There, he swore in plain text about the impossibility of migrating due to the lack of tables. I rolled back migrations to the state before the problem, removed migrations conflicting with the current state of the database and created new ones, according to the rails conventions. In the end, everything worked.
I'm trying to write some test for my rails app. On the first line of the definition for my very first test, rails throws this unhelpful error:
Error:
LabStationTest#test_prioritize_students_basic:
ActiveRecord::StatementInvalid: Could not find table 'equipment'
equipment was a table that I deleted in a previous migration. I have no more references to it in my code, and have since then run commands like rails db:drop, rails db:migrate, rails db:reset, and rails db:test:prepare multiple times to no avail.
I tried searching project-wide for the word "equipment", and I did find a CREATE TABLE IF NOT EXISTS "equipment" statement left over in structure.sql for some reason. I'm not sure if that's relevant or how to safely delete it, and I couldn't find any other references to that term.
Rails is great but it absolutely drives me nuts how the codegen scatters hidden references all over the place. Anyone know what I should do next?
Shortly after posting this, I discovered outdated models left over in test/fixtures - this seems to be the culprit.
Errors like this are my biggest frustration with Rails, they cast doubt on the "convention over configuration" paradigm for me. It wouldn't be half as bad if they at least included a stack trace with the error messages, but even with the -v flag they tend to provide little useful information
I’m trying to add a string column to a SQLite 3 database in rails. Use the normal syntax "rails generate migration AddAuthorColumnToPublications author:string". I run the migration, it works without errors.
I change the attributes accessor in the Publications model to include the Author column. I check in the schema, the new column has been included in the schema.
I go to my database and the new column does not appear in the index of the Publications table. I’ve tried including it specifically in the index, but to no avail.
The only thing I can think of here is that because I am using Ubuntu, the syntax may be slightly different (as it has been for certain things).
I have looked in books and on the internet, and the syntax seems correct (can also use underscores as well as camel casing to name the migration), but I can’t find anything specific to Ubuntu on this particular issue.
I would appreciate any and all help on this matter.
Run this command:
rake db:migrate
Migrations are not run automatically. You need to run rake db:migrate in order to run all the migrations and update the database.
Also, review your database.yml file and make sure that you're using the sqlite database that you're opening. The syntax of the command is not different.
When I manually enter tables into the .rb file in the migrate folder nothing shows up. I have followed a few different "how to's" but I can't get the tables to show up when I preview my work. I can however get one table to show up if I create it in the parameters when generating a scaffold, but that's one table. I have Rails version 2.3.4 and version 1.2.3 installed on my lame Windows Vista. I have tried using both versions multiple times. I am also trying to follow the Ruby on Rails for Dummies book, but it seems a little outdated. Any tips on getting tables to work? Its supposed to be really simple and take "just minutes" to complete this simple app. Maybe something didn't install right? Maybe a better book?
Just want to make sure you're using all the tools available to you:
To generate a migration file:
./script/generate MigrationFileName
Take a look at the Database Migrations Guide from the official Rails Guides for the correct syntax and options for creating a migration file.
Once you've created your migration, you need to run the following:
rake db:migrate
This will populate the database with the information specified in the migration file.
I have two databases in use in a Ruby on Rails application; one is the database for the application while the second is an independent database over which Rails is not given control.
Problem is when loading fixtures into the dev, it tries to run DELETE statements on the tables in the independent database from the connection to the dev database, which obviously errors out.
I don't want Rails to try to do ANYTHING but read the independent database - I especially don't want it trying to delete tables.
Is there a simple way to tell Rails to ignore the models for the second database when loading fixtures?
UPDATE: To clarify, Rails seems to think the tables from the independent database are part of the development connection, though I have specified the correct connection in the model class using establish_connection. As another note, all model classes work precisely as desired from script/console.
rake db:fixtures:load RAILS_ENV=testing
will do the job for database configured as testing in your database.yml
I think you might also be able to accomplish this by adding all the tables in the independent database to ActiveRecord::SchemaDumper.ignore_tables in environment.rb, like so:
ActiveRecord::SchemaDumper.ignore_tables = ['independent_db_table1', 'independent_db_table2']
Okay...my issue was that I used the script/generate to create the models from the secondary database, which also created fixture, schema, test, and migrations files. I had removed the schema, test, and migrations files, but did not remove the generated fixtures (they were empty files) as I did not think it had created any.
After removing all files (including the models) from the secondary database and re-running the migrations for the dev db, I added back only the model files and the database in databases.yml from the secondary db, which solved the issue.
I still can't explain why Rails' rake tasks were looking in the wrong database and I am a little disappointed with rails' addition of the schema_migrations table in the secondary database, which it obviously does not need.
However, it works now.
Delete the model_name.yml file in your test/fixtures directory and Rails will not try to delete these tables.
Better yet, delete all your *.yml files and stop using fixtures entirely.