Tag: rails migrations

数据库特定迁移代码

我正在创建一个需要在多个数据库下运行的应用程序。 我目前在迁移中有一些代码,我只想在特定的数据库(postgresql和mysql)下运行。 有任何设置方法吗? 谢谢。

ID列的Rails迁移从1,000开始并从那里自动增加?

我希望我的订单模型的ID从1000开始,并从那里自动递增计数。 这可以通过迁移来完成吗?

如何在Rails应用程序中禁用迁移function?

背景 我们分别设计数据库模型和应用程序模型(RDMBS架构师与OOP工程师)。 从我所看到的Rails与域/密钥正常forms相比,Rails迁移不能轻易复制精心设计的企业RDBMS的所有function(如果有的话),因此我们不迁移,而是使用其他工具来构建数据库(没关系对象 – 关系阻抗不匹配的问题)。 对于我们来说,数据完整性和数据库性能对于冒任何开发人员更改RDBMS模型的风险来说太宝贵 题 无论出于何种原因,我们现在都有一个Rails应用程序,它通过迁移破坏了数据库更改。 如何在现有Rails应用程序中干净地禁用此function? 我有我的理论,但我想知道世界的想法。

Rails:rake db:create:all(无法连接到服务器)

按照截屏videohttp://railscasts.com/episodes/342-migrating-to-postgresql?autoplay=true执行 “rake db:create:all”的步骤并获取错误: 无法连接到服务器:没有这样的文件或目录服务器是否在本地运行并接受Unix域套接字“/tmp/.s.PGSQL.5432”上的连接? 请参阅Rails上的问题:rake db:create:all无法连接到PostgreSQL数据库 但仍然无法解决它。 不确定是什么问题。 [database.yml的] development: adapter: postgresql encoding: unicode database: store_development pool: 5 username: amysukumunu password: test: adapter: postgresql encoding: unicode database: store_test pool: 5 username: amysukumunu password:

如何将序列添加到迁移并在模型中使用它们?

我希望拥有一个普通主键的“ Customer ”模型和另一个存储自定义“客户编号”的列。 另外,我希望db处理默认的客户编号。 我认为,定义序列是最好的方法。 我使用PostgreSQL。 看看我的迁移: class CreateAccountsCustomers true t.integer :salutation, :limit => 1 t.string :cp_name_1 t.string :cp_name_2 t.string :cp_name_3 t.string :cp_name_4 t.string :name_first, :limit => 55 t.string :name_last, :limit => 55 t.timestamps end say “Adding NEXTVAL(‘customer_no_seq’) to column cust_id” execute “ALTER TABLE accounts_customers ALTER COLUMN customer_no SET DEFAULT NEXTVAL(‘customer_no_seq’);” end def down drop_table […]

如何在Rails迁移中将列(包含内容)移动到另一个表?

我需要将一些列从一个现有表移动到另一个表。 如何使用rails迁移执行此操作? class AddPropertyToUser < ActiveRecord::Migration def self.up add_column :users, :someprop, :string remove_column :profiles, :someprop end def self.down add_column :profiles, :someprop, :string remove_column :users, :someprop end end 以上只是创建新列,但值保持为空… 我想避免登录到数据库以手动更新表。 如果有一种以编程方式移动列值的方法,那么性能特征是什么? 它会逐行进行,还是有办法大量更新?

Rails 4:schema.db显示“无法转储表”事件“因为跟随NoMethodError #nunfined方法`’为nil:NilClass”

我一直在研究一个带有sqlite(Rails开发环境的默认设置)的Rails 4.0应用程序,用于事件(黑客马拉松),它有一个父模型,Event,可以有很多Press_Blurbs。 首先,我运行了一些脚手架生成器,这些生成器创建了一些我看似没有问题的迁移: class CreateEvents < ActiveRecord::Migration def change create_table :events do |t| t.string :city t.string :theme t.datetime :hackathon_start t.datetime :hackathon_end t.datetime :show_start t.datetime :show_end t.text :about t.string :hack_rsvp_url t.string :show_rsvp_url t.timestamps end end end class CreatePressBlurbs < ActiveRecord::Migration def change create_table :press_blurbs do |t| t.string :headline t.string :source_name t.string :source_url t.string :logo_uri t.timestamps end […]

Rails迁移不会更改schema.rb

我有一个rails应用程序没有应用到我的schema.rb。 迁移应该创建一个表: class CreateUserGraphs < ActiveRecord::Migration def change create_table :user_graphs do |t| t.string :name t.string :content t.integer :user_id t.string :type_id t.integer :upload_id t.timestamps end add_index :user_graphs, [:user_id, :created_at] end end 我做了db:reset。 然后我尝试了rake db:migrate:up VERSION = 123123123(这是迁移#)。 我在我的“开发”环境中。 为什么迁移不会影响schema.rb?

解决Rails孤立迁移的最佳方法是什么?

我一直在项目中的分支之间切换,每个分支都有不同的迁移…这是场景: $ rake db:migrate:status Status Migration ID Migration Name ————————————————– … up 20130307154128 Change columns in traffic capture up 20130311155109 Remove log settings up 20130311160901 Remove log alarm table up 20130320144219 ********** NO FILE ********** up 20130320161939 ********** NO FILE ********** up 20130320184628 ********** NO FILE ********** up 20130322004817 Add replicate to root settings up […]

Rails:创建索引时“t.references”不起作用

class CreateBallots < ActiveRecord::Migration def change create_table :ballots do |t| t.references :user t.references :score t.references :election t.string :key t.timestamps end add_index :ballots, :user add_index :ballots, :score add_index :ballots, :election end end 结果是: SQLite3::SQLException: table ballots has no column named user: CREATE INDEX “index_ballots_on_user” ON “ballots” (“user”)/home/muhd/awesomevote/db/migrate/20130624024349_create_ballots.rb:10:in `change’ 我认为t.references应该为我处理这个问题?