【发布时间】:2011-05-13 21:14:48
【问题描述】:
我正在使用 PHP 和 PotgreSQL 为数据库创建接口, 使用 PHP 的 Active Record 实现 -> http://www.phpactiverecord.org
挺好用的,不过有个小麻烦: 当您在表中使用“now()”作为默认值定义时间戳字段时 - 这 引擎没有得到它:-(
用于查询列信息的SQL在这里:
SELECT
a.attname AS field,
a.attlen,
REPLACE
(
pg_catalog.format_type(a.atttypid, a.atttypmod),
'character varying',
'varchar'
) AS type,
a.attnotnull AS not_nullable,
i.indisprimary as pk,
REGEXP_REPLACE
(
REGEXP_REPLACE
(
REGEXP_REPLACE(s.column_default,'::[a-z_ ]+',''),
'\'$',''
),
'^\'',''
) AS default
FROM
pg_catalog.pg_attribute a
LEFT JOIN
pg_catalog.pg_class c ON(a.attrelid=c.oid)
LEFT JOIN
pg_catalog.pg_index i ON(c.oid=i.indrelid AND a.attnum=any(i.indkey))
LEFT JOIN
information_schema.columns s
ON(s.table_name=???? AND a.attname=s.column_name)
WHERE
a.attrelid = (select c.oid from pg_catalog.pg_class c inner join
pg_catalog.pg_namespace n on(n.oid=c.relnamespace) where c.relname=?????
and pg_catalog.pg_table_is_visible(c.oid))
AND a.attnum > 0
AND NOT a.attisdropped
ORDER BY a.attnum`
?????改为表名。
对于时间戳列,此代码返回 '' 或 null(我不确定是哪一个)作为默认值。如何修改它以返回“now()”甚至更好 - now() 的结果?
(对于以函数结果为默认值的任何其他列也是如此)
提前致谢!
【问题讨论】:
标签: php postgresql activerecord default-value