Search code examples
sqlgrails

Grails: Join two tables using plain sql


I want to write sql query that joins two tables and return the result. Lets say we have two domain classes:

class Patient {
    static hasMany = [visits: Visit]
    String firstName
}

and

class Visit {
    static belongsTo = [patient: Patient]
    String visitNo
}

Now, how could I join those two tables using sql?


Solution

  • Use grails schema-export to write the table DDL to target/ddl.sql - it will show you the table structure, foreign keys, etc.

    In this case it'd be something like

    select p.first_name, v.visit_no from patient p join visit v on v.patient_id=p.id