Im making an application with a pre-existing database, I set up the database.yml to use the database.
database.yml
development:
adapter: mysql2
encoding: utf8
# database: ttlem_demo_development
database: ttle
pool: 5
username: root
password:
socket: /tmp/mysql.sock
# 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:
adapter: mysql2
encoding: utf8
database: ttlem_demo_test
pool: 5
username: root
password:
socket: /tmp/mysql.sock
production:
adapter: mysql2
encoding: utf8
database: ttlem_demo_production
pool: 5
username: root
password:
socket: /tmp/mysql.sock
I only want one table out of the database it is called account views, I try to generate a scaffold for this with all the correct fields but it tells me i need to migrate (when i render it in the browser), if i migrate i wont be able to use the existing data, is this correct? How can i make a model that will use the existing data and fields?
Thank you for all your help :)
Two things to try:...
#1 Run your scaffold without a migration so it doesn't think you're missing one.
rails g scaffold yourmodelname fieldone:fieldtype ... etc --no-migration
#2 If that doesn't work you can go the long way round but dumping and reloading with a valid schema version number
Add this db to yml to your gemfile:
gem 'yaml_db', github: 'jetthoughts/yaml_db', ref: 'fb4b6bd7e12de3cffa93e0a298a1e5253d7e92ba'
It works for either rails 3 or rails 4.
Do a schema dump of your current database so you'll get a schema.rb with a valid version number.
bundle exec rake db:schema:dump
Now that you have a valid schema dump your data.
bundle exec rake db:data:dump
Drop your database (you can do it manually using the mysql commands if you prefer or run rake db:drop)
Now recreate it with your schema file.
bundle exec rake db:schema:load
Now add back your data
bundle exec rake db:data:load
Start your server and assuming your correctly matched all your data fields in your model (so the proper strong parameters are set from your scaffold) you should be good.
Related
I'm trying to import ruby on rails project to my computer but am running into all sorts of problems with the database.
Here's the situation:
I have an appname.tar.gz that I got from another developer. I extract it and relocated it to my user directory to work with it.
Next I run bundle install in the directory to install gem
dependencies.
Then I run rake db:create to create the database and
load the schema and structure from the DB folder in the same directory. This is where I'm running into all sorts of issues. When I launch the app I get an error saying DB migration pending.
structure.sql is a MySQL dump 10.13
while the database.yml file had the adapter set to postgres: adapter: postgres. Is this normal?
Is this the best way to import an existing app into your environment?
Any help will be appreciated.
To me this sounds like you got a MySQL dump and the application is configured to use postgres. I am wonderning why this is but you can try using the mysql adapter in your config/database.yml file by setting something like
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password:
host: localhost
development:
<<: *default
database: foobar_development
The interesting bit is adapter: mysql2.
I have taken this from a brand new rails application. You need mysql running for sure.
Hope this helps. If not you have to reach out to the other person to get some postgresql compatible dump.
I got it to work! Thank you all for your help! Really appreciated it.
Below are the steps that worked for me:
Copy the project to the new computer
Open terminal and change directory to the project
Run bundle install to install all the gem dependencies
Run bundle update to update installed gems
Start the Postgres server by running pg_ctl -D /usr/local/var/postgres start
Run createdb databasename to create the database on the new computer. The database name has to match the database mentioned in the database.yml file in the config folder.
Run rake db:schema:load and that's it!
After following the above steps I was able to run rails s to check out the website.
I am using ruby-2.2.4, Rails 4.2.5 and MySQL 5.7.16 with gem mysql2 in my Ruby on Rails application. I have created database with name 123_4 and set database name in /config/database.yml.
Why I am getting error ActiveRecord::NoDatabaseError: Unknown database '1234' when trying rake db:migrate?
If I try to run rake db:create database with name 1234 will be created.
If I use 123_abc4 for database name everything is fine.
my database.yml content:
production:
adapter: mysql2
database: 123_4
host: localhost
username: user
password: "pass"
encoding: utf8
Identifiers may begin with a digit but unless quoted may not consist solely of digits.
MySQL Schema Object Name
So you can use 123_abc4 because it contains letters.
If it only includes number, you will need to quote it: '123_4'
I have this project and I need to integrate a bunch of databases into my Rails project.
This is the database configuration that I have to work with.
$ nano config/database.yml
production:
adapter: mysql2
reconnect: true
pool: 5
username: user_xyz
password: 123456
database: database1
host: localhost
database_2:
adapter: mysql2
reconnect: false
pool: 5
username: user_xyz
password: 123456
database: database2
host: 192.168.2.100
database_3:
adapter: mysql2
reconnect: false
pool: 5
username: user_xyz
password: 123456
database: database3
host: 192.168.2.101
database_4:
adapter: mysql2
reconnect: false
database: database4
pool: 5
username: user_xyz
password: 123456
host: 192.168.2.102
I need to update the schema inside db/schema.rb but unfortunately it's only producing the schema for production (database1).
$ RAILS_ENV=production bundle exec rake db:schema:dump
I can't just run this as it complains:
$ bundle exec rake db:schema:dump
rake aborted!
database configuration does not specify adapter
What do I need to do to get all of these databases into db/schema.rb?
Austio's answer is a good one. Here is some extra detail on how to implement a solution. Create a new rake file, e.g., lib/tasks/schema_dump.rake, and add the following contents:
namespace :db do
namespace :schema do
desc 'dumps the schema of database_1 to db/schema_db1.rb'
task :dump_db1 => :environment do
ActiveRecord::Base.establish_connection 'database_4'
File.open(Rails.root.join('db', 'schema_db1.rb'), 'w') do |file|
ActiveRecord::SchemaDumper.dump ActiveRecord::Base.connection, file
end
end
end
end
Now when you run rake db:schema:dump it'll dump the schema of your primary database to db/schema.rb (like traditional Rails). But when you run rake db:schema:dump_db1, it'll dump the schema of your database_1 block into db/schema_db1.rb.
You can create similar tasks for your other database blocks and have a schema file for each database.
You will need to establish a connection to each database that you would like to schema dump on. Here is example for database_4
ActiveRecord::Base.establish_connection 'database_4'
Then when you run schema dump, it will dump the current database you have a connection with. I'm not sure if the rake task has arguments to copy it to a different file name, so you may have to rename the file.
Havent come across this before, i am generating a model in rails
rails g model Rating ratings:integer
at this point no migration file is being generated
I then run
rake db:migrate
Nothing happens at this point and the model doesn’t get written a table even though myapp_development and myapp_test exists
So i checked mysql, logged in with just
mysql
and it logged straight in, neither database existed in there, I then logged in again but this time with this command
mysql -u root
and in here my databases exist. Does anyone know what is going on here and how do i get my models to create a corresponding table?
database.yml
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: recipes_development
pool: 5
username: root
password:
Thanks
Check the timestamps on your migration file and your db/schema.rb file. If you've been working on different branches of git recently, you could have problems with the timestamps.
If this is the case, try deleting your schema.rb file, then re-running rake db:migrate
your generator looks ok.
1.
it is for me (Rails 3.2):
$ rails g model Rating ratings:integer
invoke active_record
create db/migrate/20130218203713_create_ratings.rb
create app/models/rating.rb
invoke test_unit
create test/unit/rating_test.rb
create test/fixtures/ratings.yml
$ cat db/migrate/20130218203713_create_ratings.rb
class CreateRatings < ActiveRecord::Migration
def change
create_table :ratings do |t|
t.integer :ratings
t.timestamps
end
end
end
try rake db:migrate --trace
UPD
2. and you can check schema_migrations table in DB.
I am having trouble in rails, I have just installed it but when I update after updating mysql settings and running
rake db:create
and then
rails server
It started server and then when I tried viewing it via browser there errors saying active record connection not established error in strange way. I am new to both ruby and rails so that's why not understanding by debugging info. I assume that there is some thing wrong in MySQL configuration. I am using it on windows and using railsinstaller and using MySQL that came with XAMPP.
So can anyone tell that what is wrong with it and how can it be solved? Or is it better to use Linux for RoR? I do many things on windows thats why if there will be some solution at windows then that would be helpeful.
thanks for your time, following is attached output image.
I also observed that rake db:create command is not creating db, I had to do this manually. Following is my configurations for db:
adapter:mysql2
host:localhost
encoding:utf8
database:kaasib_new
pool:5
username:root
password:~
So is this fine? I don't have password on local machine db and do I need to mention 3306 for mysql in it?
A couple of things to try:
If the tilde character in the password field above is a typo, ok,
but there shouldn't be anything there.
Not sure if it's a function
of posting here, but Whitespace matters in YAML files. It should be
set up with indents like below (socket is optional):
.
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: app_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
Open Gemfile from your project.
add line => gem 'mysql2'
run command => bundle update
restart your server.