2012-03-30 7 views
1

Ich habe eine harte Zeit zu versuchen, "hasMany" in Grails 2.0.1 mit PostgreSQL 9.1 zu arbeiten. Ich habe zwei Tabellen:Seltsames Problem mit hasMany in Grails mit PostgreSQL. "FEHLER: Spalte <column_name> existiert nicht"

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; 

und zwei Domain-Klassen:

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`' 
    } 
} 

Ich habe erzeugten Ansichten und Controller für beide von ihnen und wenn ich versuche, eine bestimmte Frage zu browsen ich die folgende Fehlermeldung erhalten:

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

mit Stack-Trace:

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 

Ich habe in den letzten paar Tagen viel Gymnastik gemacht und nichts scheint zu helfen, wenn ich die Assoziation entferne funktioniert alles gut. Fehle ich etwas Offensichtliches?

+0

Ein Kommentar: Wenn Sie für Probleme suchen, verwenden Sie Groß- und Klein Bezeichner für Ihren Tabellennamen und Spaltennamen. Mixed-Case wird Sie dazu zwingen, alle Identifikatoren zu zitieren, und Ihr Front-End und Middleware müssen sich konsistent mit den Zitaten auseinandersetzen. Außerdem: Ich würde keine Kennung mit einem führenden Unterstrich ("_QUESTIONS") verwenden. Ich weiß nicht einmal, ob es legal ist, aber ich würde es nie versuchen. – wildplasser

Antwort

0

Ich habe es geschafft, dies herauszufinden. Es stellte sich heraus, dass ORM versucht, auf die Assoziationsspalte über seinen niedrigeren Namen zuzugreifen, wobei der Name geändert wurde, um das Problem zu lösen.

+0

Das habe ich gesagt. JUST_ DONT_ DO_ IT_. – wildplasser

+0

Das hat nichts damit zu tun, was du gesagt hast. –

1

Das Problem scheint eine Diskrepanz zwischen den Annahmen zu sein, die durch das Grails/GORM-Mapping gemacht wurden, und dem SQL, das zum Erstellen der Tabellen verwendet wurde.

Wenn Sie die Anführungszeichen um die Tabellen- und Spaltennamen im obigen SQL weglassen, wird in Tabellen und Spalten die Groß-/Kleinschreibung nicht beachtet. Von http://wiki.postgresql.org/wiki/Things_to_find_out_about_when_moving_from_MySQL_to_PostgreSQL:

Database, table, field and columns names in PostgreSQL are case-independent, unless you created them with double-quotes around their name, in which case they are case-sensitive. In MySQL, table names can be case-sensitive or not, depending on which operating system you are using.

Grails aus der Gleichung nehmen, wenn Sie die Tabelle mit dem folgenden erstellen:

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

Dann:

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) 

Wenn Sie jedoch die Tabelle erstellen, ohne die Anführungszeichen um Tabellen- und Spaltennamen:

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

:

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"; 
       ^