【问题标题】:Grails GORM: Change foreign key column nameGrails GORM:更改外键列名
【发布时间】:2015-03-25 18:41:31
【问题描述】:

我正在尝试更改Visitor 表中用于User id 的外键列名。该列现在命名为user_id,我想将其更改为who_id。

最小的User 域类:

class User {

    static hasMany = [
        visitor: Visitor
    ]

    String uid

    ...

}

最小的Visitor 域类:

class Visitor {

  static belongsTo = [user: User]

  ....

}

问题:

我尝试过使用mappedBy,但没有成功,是否有另一种方法可以将User 中的属性用作Visitor 中的外键?

【问题讨论】:

    标签: grails grails-orm jointable


    【解决方案1】:

    我想你想使用映射静态块:

    class Visitor {
      static belongsTo = [user: User]
      static mapping = { user column: 'who_id' }
    }
    

    【讨论】:

      【解决方案2】:

      您可以将 User 中的 uid 标记为 id(主键)。这将使其自动成为访客域中的外键。

      class User {
      
      String uid
      
      static mapping = {
          id column: 'uid'
      }
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-15
        • 2021-02-07
        • 2011-10-04
        相关资源
        最近更新 更多