【问题标题】:Grails: how to identify the primary key of a domain?Grails:如何识别域的主键?
【发布时间】:2014-09-10 14:24:10
【问题描述】:

使用 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”,因为它是该域/数据库表的主键。

感谢任何帮助,

马丁

【问题讨论】:

    标签: postgresql grails


    【解决方案1】:

    此信息可从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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-04
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      相关资源
      最近更新 更多