【问题标题】:Weird issue with hasMany in Grails using PostgreSQL. "ERROR: column <column_name> does not exist"使用 PostgreSQL 的 Grails 中 hasMany 的奇怪问题。 “错误:列 <column_name> 不存在”
【发布时间】:2012-03-30 08:08:28
【问题描述】:

我很难使用 PostgreSQL 9.1 让“hasMany”在 Grails 2.0.1 中工作。我有两张桌子:

CREATE TABLE "_QUESTIONS"
(
  "QUESTION_ID" bigint NOT NULL,
  "TEXT" text,
  CONSTRAINT "PK" PRIMARY KEY ("QUESTION_ID" )
)
WITH (
  OIDS=FALSE
);
ALTER TABLE "_QUESTIONS"
  OWNER TO postgres;

CREATE TABLE "_ANSWERS"
(
  "ANSWER_ID" bigint NOT NULL,
  "TEXT" text,
  "QUESTION_ID" bigint,
  CONSTRAINT "PK1" PRIMARY KEY ("ANSWER_ID" ),
  CONSTRAINT "FK" FOREIGN KEY ("QUESTION_ID")
      REFERENCES "_QUESTIONS" ("QUESTION_ID") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE "_ANSWERS"
  OWNER TO postgres;

还有两个领域类:

class Question {
    String text

    String toString(){
        text
    }

    static constraints = {
    }    
    static hasMany = [answers: Answer]    
    static mapping = {
        table '`_QUESTIONS`'
        version false
        id generator: 'identity'
        id column: '`QUESTION_ID`'
        text column: '`TEXT`'
    }
}

class Answer {
    String text
    Question question

    String toString(){
        text
    }

    static constraints = {
    }

    static  belongsTo = [question : Question]

    static mapping = {
        table '`_ANSWERS`'
        version false
        id generator: 'identity'
        id column: '`ANSWER_ID`'
        text column: '`TEXT`'
        question column: '`QUESTION_ID`'
    }
}

我已经为它们生成了视图和控制器,当我尝试浏览特定问题时,我收到以下错误:

URI:/hasManyTest/question/show/1
Class:org.postgresql.util.PSQLException
Message:ERROR: column answers0_.question_id does not exist Position: 8

带有堆栈跟踪:

Line | Method
->>    8 | runWorker in \grails-app\views\question\show.gsp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

Caused by SQLGrammarException: could not initialize a collection: [hasmanytest.Question.answers#1]
->>   26 | doCall    in C__Users_root_IdeaProjects_hasManyTest_grails_app_views_question_show_gsp$_run_closure2
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     55 | run       in C__Users_root_IdeaProjects_hasManyTest_grails_app_views_question_show_gsp
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . in java.lang.Thread

Caused by PSQLException: ERROR: column answers0_.question_id does not exist
  Position: 8
->> 2103 | receiveErrorResponse in org.postgresql.core.v3.QueryExecutorImpl
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1836 | processResults in     ''
|    257 | execute . in     ''
|    512 | execute   in org.postgresql.jdbc2.AbstractJdbc2Statement
|    388 | executeWithFlags in     ''
|    273 | executeQuery in     ''
|     96 | executeQuery in org.apache.commons.dbcp.DelegatingPreparedStatement
|     26 | doCall    in C__Users_root_IdeaProjects_hasManyTest_grails_app_views_question_show_gsp$_run_closure2
|     55 | run . . . in C__Users_root_IdeaProjects_hasManyTest_grails_app_views_question_show_gsp
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run       in java.lang.Thread

过去几天我做了很多体操,但似乎没有任何帮助,但当我删除关联时,一切正常。我错过了一些明显的东西吗?

【问题讨论】:

  • 一条评论:如果您正在寻找麻烦,请为您的表名和列名使用混合大小写的标识符。混合大小写将迫使您引用所有标识符,并且您的前端和中间件必须一致地处理引号。另外:我不会使用带有前导下划线(“_QUESTIONS”)的标识符。我什至不知道这是否合法,但我永远不会尝试。

标签: postgresql grails grails-orm


【解决方案1】:

问题似乎是 Grails/GORM 映射所做的假设与用于创建表的 SQL 之间的不匹配。

如果在上面的 SQL 中省略表和列名的引号,表和列将不区分大小写。来自http://wiki.postgresql.org/wiki/Things_to_find_out_about_when_moving_from_MySQL_to_PostgreSQL

PostgreSQL 中的数据库、表、字段和列的名称是大小写无关的,除非您在它们的名称周围使用双引号创建它们,在这种情况下它们是区分大小写的。在 MySQL 中,表名可以区分大小写,也可以不区分大小写,具体取决于您使用的操作系统。

如果您使用以下方法创建表,则将 Grails 排除在外:

CREATE TABLE "_QUESTIONS"
(
  "QUESTION_ID" bigint NOT NULL,
  "TEXT" text,
  CONSTRAINT "PK" PRIMARY KEY ("QUESTION_ID" )
)
WITH (
  OIDS=FALSE
);

然后:

test=> select * from _questions;
ERROR:  relation "_questions" does not exist
LINE 1: select * from _questions
                      ^
test=> select * from _QUESTIONS;
ERROR:  relation "_questions" does not exist
LINE 1: select * from _QUESTIONS;
                      ^
test=> select * from "_QUESTIONS";
 QUESTION_ID | TEXT 
-------------+------
(0 rows)

但是,如果您创建的表没有在表名和列名周围加上引号:

CREATE TABLE _QUESTIONS
(
  QUESTION_ID bigint NOT NULL,
  TEXT text,
  CONSTRAINT PK PRIMARY KEY (QUESTION_ID)
)
WITH (
  OIDS=FALSE
);

然后:

test=> select * from _questions;
 question_id | text 
-------------+------
(0 rows)

test=> select * from _QUESTIONS;
 question_id | text 
-------------+------
(0 rows)

test=> select * from "_QUESTIONS";
ERROR:  relation "_QUESTIONS" does not exist
LINE 1: select * from "_QUESTIONS";
                  ^

【讨论】:

    【解决方案2】:

    我已经设法弄清楚了。原来,ORM 尝试通过其小写名称访问关联列,更改名称解决了问题。

    【讨论】:

    • 我就是这么说的。只是_不要_做_它_。
    • 这与你说的无关。
    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 2010-12-07
    • 2021-04-30
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 2020-01-05
    相关资源
    最近更新 更多