【问题标题】:Grails many-to-many unknown column when sorting排序时Grails多对多未知列
【发布时间】:2015-05-28 16:06:53
【问题描述】:

在 hasMany 上使用 GORM 默认排序时遇到一个奇怪的错误。我经常在一对多中使用默认排序,从来没有遇到过问题。

请注意,当我删除两个类中的映射子句时,代码运行良好。但我真的希望在数据库中进行默认排序。我知道我也可以用 .sort{ it.id }

解决这个问题

这是我的域类:

abstract class Product {

    String nr
    String name
    static hasMany = [requirements:Requirement]

    static mapping = {
        requirements sort: 'id'
}


class Requirement {

    String name
    String imageName

    static hasMany = [products:Product]
    static belongsTo = Product

    static constraints = {
        name(nullable:false, blank:false)
        imageName(nullable:false, blank:false)
    }

    static mapping = {
        sort 'id'
    }
}

现在,当我打电话时

def product = Product.findByNr("test")
log.debug pr.requirements

我明白了

2015-05-28 17:58:03,374 [http-bio-8080-exec-6] ERROR spi.SqlExceptionHelper  - Unknown column 'requiremen0_.id' in 'order clause'
Error |
2015-05-28 17:58:03,455 [http-bio-8080-exec-6] ERROR errors.GrailsExceptionResolver  - MySQLSyntaxErrorException occurred when processing request: [GET] /testDb
Unknown column 'requiremen0_.id' in 'order clause'. Stacktrace follows:
Message: Unknown column 'requiremen0_.id' in 'order clause'

【问题讨论】:

  • 我个人从未见过像这样的多方面的sort 'id' 映射。我只在“拥有”方面使用过它。如果从 Requirement 中删除排序,它会起作用吗?
  • 是的。当我从需求中删除排序时,我得到了同样的错误。我还尝试按“名称”位排序,这是相同的错误,但出现错误消息未知列“requiremen0_.name”。

标签: mysql grails many-to-many grails-orm


【解决方案1】:

似乎排序应该应用于当前类的字段,而不是关联类的字段:https://grails.github.io/grails-doc/latest/ref/Database%20Mapping/sort.html **

对它进行排序以对当前查询的类进行排序的想法:

自定义查询结果排序依据的默认属性。 **

例如,您可以按名称对产品进行排序,但是如何按一组相关需求的 id 对产品进行排序?恕我直言,这没有多大意义。

尝试从 Product 中删除需求 sort: 'id'。

顺便说一句,你有抽象产品的具体子类吗?

【讨论】:

    猜你喜欢
    • 2015-06-21
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 2012-08-30
    • 2011-06-20
    相关资源
    最近更新 更多