美文网首页
数据迁徙

数据迁徙

作者: 风暴英雄 | 来源:发表于2014-03-31 20:09 被阅读0次
1 剖析迁移任务的构造
class CreateProducts < ActiveRecord::Migration
  def up
    create_table :products do |t|
      t.string :name
      t.text :description
 
      t.timestamps
    end
  end
 
  def down
    drop_table :products
  end
end

这个迁移任务建立了一张叫 products 的数据库表,这张表中包含一个名为 name 的 string 类型字段和一个名为 description 的 text 类型字段。与此同时,一个名为 id 的字段也会被添加,这个字段是默认添加,我们不需要另外请求。
另外 Active Record 所需要的时间戳( timestamp )字段( created_at 和 updated_at )也会被自动添加。而要取消这个任务只需简单地把这张表删除掉即可。

2.1 创建Migration
$ rails generate migration AddPartNumberToProducts

使用标准的"AddXXXToYYY" 或者"RemoveXXXFromYYY"生成时

 $ rails generate migration AddPartNumberToProducts part_number:string

会生成如下结果

class AddPartNumberToProducts < ActiveRecord::Migration
  def change
    add_column :products, :part_number, :string
  end
end

我们也可以使用migration来删除

 rails generate migration RemovePartNumberFromProducts part_number:string

会生成如下代码

class RemovePartNumberFromProducts < ActiveRecord::Migration
  def change
    remove_column :products, :part_number, :string
  end
end

添加数据

$ rails generate migration AddDetailsToProducts part_number:string price:decimal

生成如下代码

class AddDetailsToProducts < ActiveRecord::Migration
  def change
    add_column :products, :part_number, :string
    add_column :products, :price, :decimal
  end
end

如果我们使用CreateXXX 和一些列定义,会自动代码如下所示

 $ rails generate migration CreateProducts name:string part_number:string

将会生成下面的代码

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string :name
      t.string :part_number
    end
  end
end

在多对多关系型表单时,我们可以使用下面的代码生成中间表

rails g migration CreateJoinTableCustomerProduct customer product

会生成下面代码

class CreateJoinTableCustomerProduct < ActiveRecord::Migration
  def change
    create_join_table :customers, :products do |t|
      # t.index [:customer_id, :product_id]
      # t.index [:product_id, :customer_id]
    end
  end
end
2.2 模型生成
rails generate model Product name:string description:text
2.3 类型修改
  • limit Sets the maximum size of the string/text/binary/integer fields
  • precision Defines the precision for the decimal fields
  • scale Defines the scale for the decimal fields
  • polymorphic Adds a type column for belongs_to associations
  • null Allows or disallows NULL values in the column.

3 自己动手写一个Migration

相关文章

  • 数据迁徙

    1 剖析迁移任务的构造 这个迁移任务建立了一张叫 products 的数据库表,这张表中包含一个名为 name 的...

  • Population‐level scaling of avia

    迁徙水鸟powered fliers种群水平迁徙速度和身体大小以及迁徙距离的尺度效应 数据: 07-11年 ebi...

  • 2020-05-13

    执行数据库迁徙,生成伪数据 1.接着前面的步骤,我们开始执行数据库迁徙.命令是:php artisan migra...

  • 百度慧眼迁徙大数据——数据分析(1)

    一些前话 最近听了一些报告,我发现很多论文都引用了百度慧眼迁徙的大数据。百度慧眼迁徙大数据的获取也非常方便,今年百...

  • 3月18日 数字货币24小时搬砖日报

    数据库服务器升级,未能在12点前完成数据迁徙,本日日报继续暂停

  • 3月17日 数字货币24小时搬砖日报

    数据库服务器升级,未能在12点前完成数据迁徙,本日日报继续暂停

  • 迁徙啊迁徙

    回得去的是家乡,回不去的是故乡。第一次看到这句话时,就看懂了它里面的伤感。 今天在看《迁徙的鸟》,本来以为这部盛赞...

  • 区块链背景下的数据治理革命

    一、当前数据治理现状当前人类正朝着数字化时代迁徙,数据资源的价值已毋庸置疑。然而,数据隐私如何保护、数据交易和共享...

  • 迁徙

    赤灼的烈日吞噬了这片土地 低矮的灌木丛垂头丧气 连最后的垂死挣扎 都早已丧失殆尽 迁徙的角马群 依旧,蠢蠢欲动 一...

  • 迁徙

    有人从乡下迁徙到城市,有人从小城市迁徙到大城市,有人从大城市迁徙到一线大都市,有人从一线大都市迁徙到外国.......

网友评论

      本文标题:数据迁徙

      本文链接:https://www.haomeiwen.com/subject/xuoktttx.html