Grails:如何识别域的主键?

时间:2014-09-10 06:22:22

标签: postgresql grails

使用Grails 2.4.3和postgres,我需要在运行时找出域对象的主键列。

CREATE TABLE article
(
art_number character varying(255) NOT NULL,
desc character varying(255) NOT NULL,
group character varying(255) NOT NULL,
CONSTRAINT artikel_pkey PRIMARY KEY (art_number)
)

对于这个例子,我需要一段代码来返回字符串" art_number",因为它是该域/数据库表的主键。

任何帮助表示赞赏,

马丁

1 个答案:

答案 0 :(得分:0)

可以从information schema

中检索此信息
select column_name
from information_schema.key_column_usage
where table_schema = 'public' -- change here if the table is in a different schema
  and table_name = 'article' -- change here for other tables
order by ordinal_position -- if the PK has more than one column;