非主要Grails的重复进入问题 [英] Duplicate entry issue for grails on non primary

查看:64
本文介绍了非主要Grails的重复进入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是领域类

  class Requestlog {
String countryiso
Date requestdate
String requestaddr
static constraints = {
}
}

创建记录的代码

  def reqip = request.remoteAddr 
def reqdate = new Date()
def reqrec = new Requestlog(countryiso:countryiso,requestaddr:reqip ,requestdate:reqdate)
reqrec.save(flush:true,failOnError:true)

但是,如果来自同一IP的相同代码被请求了两次(在不同的时间),那么密钥'UK_3noxnln23h2w0wtormncmk2a1'的重复条目'GL'会被重复输入'GL' 。 Stacktrace如下:
消息:重复键'UK_3noxnln23h2w0wtormncmk2a1'的条目'GL'
行|方法

问题是,我没有设置任何列是唯一的。并且查看数据库模式,除id列自动递增之外没有任何限制。

解决方案

经过一段时间,我找到了问题。

关键字'UK_3noxnln23h2w0wtormncmk2a1'的引用是关键(不参与双关语)。
这是一个索引,似乎是在创建时由grails或mysql自动生成的,所以一旦我尝试在数据库中创建一个具有相同代码的新条目,它就会抛出一个异常,因为它只要求一个条目与索引匹配。



我通过mysql workbench编辑器进入并删除了它,并且我的应用程序开始工作(并且仍然有效)



知道如果是设置索引的mysql或grails,以及是否有函数现在不起作用,会很有趣。对于我来说,我只是写给这张桌子,从不阅读,所以这个索引对我来说没有什么不同。



请注意,如果您已在datasource.config上创建,则每次启动应用程序时都必须删除索引(并且不要删除名为ID的索引,它是你想要的长名字)。


Decided to add a little log that basically logs a code, the request ip and a date stamp.

This is the domain class

  class Requestlog {
    String countryiso
    Date requestdate
    String requestaddr
    static constraints = {
    }
}

And the code that creates the record

    def reqip = request.remoteAddr
    def reqdate = new Date()
    def reqrec = new Requestlog(countryiso: countryiso, requestaddr: reqip, requestdate: reqdate )
    reqrec.save(flush:true,failOnError:true)

But if the same code from same IP is requested twice (at different times)

Duplicate entry 'GL' for key 'UK_3noxnln23h2w0wtormncmk2a1'. Stacktrace follows:
Message: Duplicate entry 'GL' for key 'UK_3noxnln23h2w0wtormncmk2a1'
    Line | Method

The thing is, I am not setting any of the columns to be unique. And looking at the database schema, there is no constraints other than that the id column auto increments.

解决方案

After fiddling about for a while I found the issue.

The reference to the key 'UK_3noxnln23h2w0wtormncmk2a1' is the key (no pun attended). This is an index which seems to be auto generated by either grails or mysql at creation, so as soon as I tried to create a new entry with the same code in the database, it threw an exception because it requires that only one entry matches the index.

I went in via mysql workbench editor and deleted it, and my app started working (and is still working hopefully)

Would be interesting to know if it is mysql or grails that sets the index, and if there are functions that will not work now. For me I am only writing to this table, never reading, so the index makes little difference to me.

Please note that if you have create on datasource.config you will have to delete the index every time you start your application (and don't delete the one called ID, its the one with the long name you want).

这篇关于非主要Grails的重复进入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆