【问题标题】:Create ENUM in POSTGRESQL using Codeigniter Migrations and DBFORGE使用 Codeigniter Migrations 和 DBFORGE 在 POSTGRESQL 中创建 ENUM
【发布时间】:2019-01-07 19:58:34
【问题描述】:

我想在 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');
        }
}

但这会引发错误:

遇到 PHP 错误 严重性:警告

消息:pg_query():查询失败:错误:类型“枚举”不存在 第 6 行:"is_default" ENUM("a","b","c") DEFAULT 'no' NULL ^

文件名:postgre/postgre_driver.php

行号:242

回溯:

【问题讨论】:

  • 我不知道 CI,但通常情况下,在 PostgreSQL 中,您必须先创建枚举类型,然后将其用作字段类型 - 请参阅 postgresql.org/docs/current/datatype-enum.html
  • @Furgas 是的,我知道,我只是想知道 CI 是否有能力代表我这样做,如果没有,那么 DB Forge 库就没用了
  • 我不知道为什么这被否决了!如果您有答案,请给出答案,而不是仅仅投票否决问题
  • 我猜你首先必须使用纯 SQL 来创建枚举类型(可能使用 $this->db->query(...))然后你可以在 @987654323 中使用它的名称@类型。
  • 是的,我相信,我会导入 SQL 或 CI Query Builder 来创建枚举类型,然后进行 CI 迁移

标签: php postgresql codeigniter enums


【解决方案1】:

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

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

【讨论】:

  • 我不知道 PostgreSQL 但我知道 CI,在 CI3 中您可以使用 dbforge 添加枚举字段,我将它 (v3.1.11) 与 MySQL 一起使用。附言我也一直在尝试将它与 SQlite 一起使用,但不,SQlite 似乎没有枚举类型。
  • @AdemTepe 是的,在 MySQL 中,您可以动态为特定表列定义 ENUM 类型。在 PostgreSQL 中,您必须先创建枚举类型,然后才能在列定义(或多个列)中将其作为列类型引用。
【解决方案2】:

在枚举中你不能传递空数据...这是sql错误...更改is_default数组

'null' => TRUE,

'null' => FALSE,

【讨论】:

  • set 为 null 为默认值...如果它没有捕获值...那么默认值发生...如果您没有设置默认值...那么它变为 null .. . 你不能将枚举字段上传为 null ...
猜你喜欢
  • 2014-01-22
  • 2020-05-22
  • 2013-05-06
  • 2016-02-23
  • 2014-01-31
  • 1970-01-01
  • 2022-12-07
  • 1970-01-01
相关资源
最近更新 更多