php - 使用 Codeigniter 迁移和 DBFORGE 在 POSTGRESQL 中创建 ENUM

标签 php postgresql codeigniter enums

我想在 postgres 数据库中创建一个带有 ENUM 字段的表。

通常,当我们在 postgresql 中创建枚举时,我们命名枚举和我们想要设置枚举的列,选择该枚举名称。但我如何在 Codeigniter 中执行此操作?

下面是我的代码:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Admin_Locations extends CI_Migration {

        public function up()
        {

                $this->dbforge->add_field(array(
                        'id' => array(
                                'type' => 'INT',
                                'null' => FALSE,
                                'auto_increment'=>TRUE,
                                'constraint' => 11,
                        ),
                        'location_name' => array(
                                'type' => 'VARCHAR',
                                 'constraint' => 50,
                                 'null' => TRUE,
                        ),
                        'tenant_id' => array(
                                'type' => 'INT',
                                'null' => TRUE,
                        ),
                        'description' => array(
                                'type' => 'VARCHAR',
                                 'constraint' => 128,
                                 'null' => TRUE,
                        ),
                        'is_default' => array(
                                'type' => 'ENUM("a","b","c")',
                                'default' => "a",
                                'null' => TRUE,
                        ),
                ));

            $this->dbforge->add_key('id');
            $this->dbforge->create_table('admin_locations');
            $query = $this->db->get('admin_locations');
            $res=$query->num_rows();

             if($res==0)
             {
                $data = array(
                        'id'=>"1",
                        'location_name'=>"default site",
                        'tenant_id'=>"1",
                        'description'=>"This is default site for portal",
                        'is_default'=>"a",
                     );
                $this->db->insert('admin_locations', $data); 
            }

        }
        public function down()
        {
          $this->dbforge->drop_table('admin_locations');
        }
}

但这会引发错误:

A PHP Error was encountered Severity: Warning

Message: pg_query(): Query failed: ERROR: type "enum" does not exist LINE 6: "is_default" ENUM("a","b","c") DEFAULT 'no' NULL ^

Filename: postgre/postgre_driver.php

Line Number: 242

Backtrace:

最佳答案

在 PostgreSQL 中,您必须首先创建枚举类型,然后将其用作字段类型。参见 PostgreSQL 枚举类型 documentation供引用。

我不太了解 CI,但我看到了 Database Forge class不支持创建枚举类型,因此您必须首先使用纯 SQL 或某些 CI SQL 构建器创建它,然后您可能可以在 add_field 中使用该枚举类型名称。方法。

关于php - 使用 Codeigniter 迁移和 DBFORGE 在 POSTGRESQL 中创建 ENUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54071841/

相关文章:

php - NetBeans + XDebug 断点不起作用

php - 使用 Smarty 模板发送电子邮件

php - 为什么我的 AJAX 调用在 Internet Explorer 中编辑返回的文本?

php - CodeIgniter 中的多个表更新

php - CodeIgniter HMVC object_to_array() 错误

php - 从表单提交 Sql 更新

sql - 使用连接和 "ghost"引用优化 postrgeSQL 查询?

排除某些值的 SQL 约束?

java - Hibernate 继承模型的级联删除

php - Facebook JS SDK 没有设置 cookie?