Announcement Announcement Module
Collapse
No announcement yet.
Gorm Custom ORM mapping one-to-many relationship foreign key Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Gorm Custom ORM mapping one-to-many relationship foreign key

    We have an existing mysql db I am trying to map into Grails domain. An appointment has many appointment histories, and is mapped to appointment_history by compentity_id column in db table. I am trying to define one-to-many relationship in Grails, but it creates an apponitment_id column in appointment_history table. Here is how the domains look like:

    class Appointment {

    Date created
    Date updated
    String status

    String title
    Date startDate
    Date endDate
    Integer customerId
    Integer createdUserId
    String itemType
    Integer opportunityId
    String note
    String timeZone

    static hasMany = [histories: AppointmentHistory, notes: AppointmentNotes, ibUsers : ImplantBaseUser]
    static belongsTo = [ImplantBaseUser]

    static constraints = {
    }

    static mapping = {
    histories column:'compentity_id'
    note type: 'text'
    }
    }


    class AppointmentHistory {
    Date created
    Integer userId
    String userName
    String action
    String description


    static belongsTo = [appointment: Appointment]

    static constraints = {
    }

    static mapping = {
    description type: 'text'
    }
    }

    And tables:

    CREATE TABLE `appointment` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `created` datetime DEFAULT NULL,
    `updated` datetime DEFAULT NULL,
    `status` varchar(25) DEFAULT NULL,
    `published` datetime DEFAULT NULL,
    `activated` datetime DEFAULT NULL,
    `title` varchar(255) DEFAULT NULL,
    `start_date` datetime DEFAULT NULL,
    `end_date` datetime DEFAULT NULL,
    `notes` text,
    `customer_id` int(11) DEFAULT NULL,
    `created_user_id` int(11) DEFAULT NULL,
    `item_type` varchar(255) DEFAULT NULL,
    `opportunity_id` int(11) DEFAULT NULL,
    `note` text,
    `time_zone` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id`)
    )

    CREATE TABLE `appointment_history` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `created` datetime DEFAULT NULL,
    `compentity_id` int(11) DEFAULT '0',
    `user_id` int(11) DEFAULT '0',
    `user_name` varchar(255) DEFAULT NULL,
    `action` varchar(25) DEFAULT NULL,
    `description` text,
    PRIMARY KEY (`id`),
    KEY `appointment_history_i_compentity_id` (`compentity_id`)
    )

    What am I missing here? All I am trying to do, is, ask Grails to use compentity_id as foreign key (= appointment.id) and not generate an appointment_id column in appointment_history table..any idea?
Working...
X