• Rails向数据库添加新字段


    控制台上执行下面的命令

    rails g migration addColumnToBlackIps send_time:datetime
    

    会生成文件db/migrate/20210529131328_add_column_to_black_ips.rb

    class AddColumnToBlackIps < ActiveRecord::Migration[5.0]
      def change
        add_column :black_ips, :send_time, :datetime
        add_column :black_ips, :black_tools, :boolean, default: false ,comment:'黑客工具 false:否 true:是'
      end
    end
    
    

    执行迁移

    rake db:migrate

    执行结果

    CREATE TABLE `black_ips` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `ip` varchar(255) DEFAULT NULL,
      `created_at` datetime NOT NULL,
      `updated_at` datetime NOT NULL,
      `send_time` datetime DEFAULT NULL,
      `black_tools` tinyint(1) DEFAULT '0' COMMENT '黑客工具 false:否 true:是',
      PRIMARY KEY (`id`),
      KEY `index_black_ips_on_ip` (`ip`)
    ) ENGINE=InnoDB AUTO_INCREMENT=6121 DEFAULT CHARSET=utf8;
    

    看一下表里会在schema_migrations表里添加一条已经迁移过的记录

    值会和迁移文件名一致。

    [Haima的博客] http://www.cnblogs.com/haima/
  • 相关阅读:
    GO make&new区别
    GO 包相关
    GO 类型断言
    栈 队列 链表
    表达式求值
    动态规划 最长子序列
    04 单例模式
    02 简单工厂模式
    java设计模式 01 开山篇
    java基础07 多线程
  • 原文地址:https://www.cnblogs.com/haima/p/14826408.html
Copyright © 2020-2023  润新知