使用MYSQL时,Grails条件查询失败,语法错误

问题描述 投票:0回答:2

当应用程序连接到MYSQL实例时,我的查询失败。使用H2内存数据库时,查询工作正常。所有其他查询都可以使用MYSQL。

错误消息:您的SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以便在第1行的')'附近使用正确的语法

控制器动作:

def index() {
def currentUser = currentUser()
def peopleFollowing = currentUser.following
def c = Post.createCriteria()
    def postList = c.list  {
       'in' ("user", peopleFollowing)
       'order' "dateCreated", "desc"
    }
[postList:postList, user : currentUser, peopleFollowing:peopleFollowing]
}

当我删除in子句时,查询将针对MYSQL运行。所以似乎:'in'(“用户”,peopleFolllowing)导致了这个问题。我认为问题在于它是一个MYSQL保留字。我尝试过使用反引号,但一直都会出现语法错误......?

class Post {


String content
Date dateCreated
User user


static belongsTo = [user : User]

static hasMany = [postComments: PostComment]

static constraints = {
    content (blank: false)
}

static mapping = {
    sort dateCreated:"desc"
    content type:"text"
    postComments sort:"dateCreated",  order:"desc"
}

}

class Post {String content Date dateCreated User user static belongsTo = [user:User] static hasMany = [postComments:PostComment] static constraints = {content(blank:false)} static mapping = {sort dateCreated:“desc”content type:“ text“postComments sort:”dateCreated“,order:”desc“}} class Post {String content Date dateCreated User user static belongsTo = [user:User] static hasMany = [postComments:PostComment] static constraints = {content(blank:false) )} static mapping = {sort dateCreated:“desc”内容类型:“text”postComments sort:“dateCreated”,order:“desc”}}

grails
2个回答
0
投票

我通过这样做来解决问题:

def postList = Post.findAllByUserInList( peopleFollowing )

0
投票

我使用ids解决了它,并在0中添加了一个List

def currentUser = currentUser()
def peopleFollowingIds = currentUser.following.id
peopleFollowingIds.add(0)
def c = Post.createCriteria()
def postList = c.list  {
    'in' ("user_id", peopleFollowingIds)
    'order' "dateCreated", "desc"
}

或类似的东西。

© www.soinside.com 2019 - 2024. All rights reserved.