【发布时间】: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