ThinkPHP Migrate

安装

composer require topthink/think-migration

创建迁移文件

php think migrate:create TableName

 

执行迁移文件

up:在migrate:run时执行(前提是文件中不存在change方法)

down:在migrate:rollback时执行(前提是文件中不存在change方法)

change:migrate:run 和migrate:rollback时执行 (如果存在该方法 则不会去执行up 与down)

 

新增表

public  function  change()
    {
       $this->table('users',array('engine'=>'MyISAM'));//指定引擎
           //字段名    字段类型     其他属性
->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用户名,登陆使用')) ->addColumn('password', 'string',array('limit' => 32,'default'=>md5('123456'),'comment'=>'用户密码')) ->addColumn('login_status', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'登陆状态')) ->addColumn('login_code', 'string',array('limit' => 32,'default'=>0,'comment'=>'排他性登陆标识')) ->addColumn('last_login_ip', 'integer',array('limit' => 11,'default'=>0,'comment'=>'最后登录IP')) ->addColumn('last_login_time', 'datetime',array('default'=>0,'comment'=>'最后登录时间')) ->addColumn('is_delete', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'删除状态,1已删除')) ->addIndex(array('username'), array('unique' => true)) ->create(); }

字段类型对应

integer -> int
biginteger -> bigint
string -> varchar

其他属性定义

primary_key 指定主键 表属性

limit 字段长度
(配合使用) precision 字段总长 scale 小数点
comment 字段注释
default 默认值 null 允许null值,只能为 true / false unique 创建唯一索引

 执行函数

create 创建表
update 更新表
drop 删除表

 

posted @ 2021-11-12 16:51  悬剑丶  阅读(315)  评论(0编辑  收藏  举报