rails-migrations

Rails 3.1: can't write to column in same migration that adds it

≯℡__Kan透↙ 提交于 2019-12-20 10:26:43
问题 I had an add_column migration that would run fine. However, after running it and firing up a console, I would find the first_name and last_name columns completely empty. I tried using save! instead and it had the same effect--no errors reported. Here's the original: class UserAddFirstNameAndLastName < ActiveRecord::Migration def change # add column first name, last name string add_column :users, :first_name, :string add_column :users, :last_name, :string User.all.each do |u| u.first_name =

Destroy/Remove database in Rails

假如想象 提交于 2019-12-20 09:53:35
问题 Is it possible to completely remove the database and all migration records etc from an existing application so I can redesign the database from scratch? 回答1: By issuing rake -T you have the following database tasks: rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config) rake db:drop # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases) rake db

What's the correct syntax for remove_index in a Rails 3.1.0 migration?

ぐ巨炮叔叔 提交于 2019-12-20 09:09:30
问题 I'm in the process of adding Devise to an existing Rails app, with a Users table already defined. The devise generator pushed out the following migration: class AddDeviseToUsers < ActiveRecord::Migration def self.up change_table(:users) do |t| ## Database authenticatable t.string :email, :null => false, :default => "" t.string :encrypted_password, :null => false, :default => "" ## Recoverable t.string :reset_password_token t.datetime :reset_password_sent_at ## Rememberable t.datetime

Ruby on Rails: adding columns to existing database

依然范特西╮ 提交于 2019-12-20 08:16:11
问题 I'm getting an error: SQLite3::SQLException: no such column: ideas.list_id: SELECT "ideas".* FROM "ideas" WHERE "ideas"."list_id" = 2 But I added t.integer :list_id to my db migration file: class CreateIdeas < ActiveRecord::Migration def change create_table :ideas do |t| t.string :name t.text :description t.string :picture t.timestamps end add_foreign_key :ideas, :lists end end which gave me this: class CreateIdeas < ActiveRecord::Migration def change create_table :ideas do |t| t.string :name

Ruby on Rails: adding columns to existing database

流过昼夜 提交于 2019-12-20 08:16:04
问题 I'm getting an error: SQLite3::SQLException: no such column: ideas.list_id: SELECT "ideas".* FROM "ideas" WHERE "ideas"."list_id" = 2 But I added t.integer :list_id to my db migration file: class CreateIdeas < ActiveRecord::Migration def change create_table :ideas do |t| t.string :name t.text :description t.string :picture t.timestamps end add_foreign_key :ideas, :lists end end which gave me this: class CreateIdeas < ActiveRecord::Migration def change create_table :ideas do |t| t.string :name

Check if a table exists in Rails

梦想的初衷 提交于 2019-12-20 08:03:06
问题 I have a rake task that won't work unless a table exists. I'm working with more than 20 engineers on a website so I want to make sure they have migrated the table before they can do a rake task which will populate that respective table. Does AR have a method such as Table.exists ? How can I make sure they have migrated the table successfully? 回答1: In Rails 5 the API became explicit regarding tables/views, collectively data sources . # Tables and views ActiveRecord::Base.connection.data

Can I delete all migration files and start from scratch?

耗尽温柔 提交于 2019-12-20 04:32:12
问题 I have a Rails-API app that I'm going to re-deploy after a long time. The app is non-production, but I am ready to deploy the production version. I want to basically delete all the migration files and start from scratch using the schema, is there any problem with this approach? Assuming I can do this, what do I need to alter the schema.rb to? Is this a common practice? Seems reasonable to me. # what do I change the version param value to? ActiveRecord::Schema.define(version: 20171129023538)

Rails: rake db:create:all (could not connect to server)

筅森魡賤 提交于 2019-12-19 09:24:37
问题 follow the screencasts http://railscasts.com/episodes/342-migrating-to-postgresql?autoplay=true up to the steps of "rake db:create:all" and get error: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"? refer the question on Rails: rake db:create:all fails to connect to PostgreSQL database but still unable to resolve it. Not sure what is the problem. [database.yml] development: adapter:

Rails Migration for ID Column to Start at 1,000 and Autoincrement Up From There?

爱⌒轻易说出口 提交于 2019-12-19 05:17:26
问题 I'd like the ID's of my Order model to start at 1000, and count up autoincrementally from there. Can this be done via migration? 回答1: In your migration, after table has been created, update the sequence with something like this: create_table :products do |t| t.string :name # other stuff end # for Postgres execute "SELECT setval('products_id_seq', 1000)" # and for mysql ... execute "ALTER TABLE products AUTO_INCREMENT = 1000" 回答2: This has not been tested and I am not sure what db you are

How do I disable the migrations feature in a Rails app?

馋奶兔 提交于 2019-12-19 04:22:07
问题 Background We engineer database models and application models separately (RDMBS architects vs OOP engineers). From what I've seen regarding Rails versus domain/key normal form, Rails migrations cannot easily duplicate all the features of a well-designed enterprise RDBMS (if at all) so we don't migrate and instead use other tools to build databases (nevermind the problem of object-relational impedance mismatch). Data integrity and DB performance are too valuable to us to risk RDBMS model