On Sunday, December 27, 2015 at 9:47:10 AM UTC-8, Jean-Luc Derrien wrote:
>
> Hello,
>
> I'm facing an error with Sequel and sqlite described on this Gist: 
> https://gist.github.com/jderrien/279556fe2edabd9a1df1
>
> I get this error when messages are duplicated. Replacing `create` by 
> `insert` will fix the problem, but I'd like to use the timestamps plugin 
> which doesn't work with the `insert` method. Even if I get this error, data 
> are correctly saved.
>
> Is it a bug or is it just a bad use? What would be the most elegant way to 
> fix this problem?
>

I can't replicate the issue with your gist.  Here's the end of my irb 
session:

 irb(main):030:0> Topic.each do |topic|
irb(main):031:1*   topic.parse
irb(main):032:1> end
==> Parse topic 'http://foo.bar/topic1'
    Import message: message-1
I, [2015-12-27T11:04:29.762455 #31282]  INFO -- : (0.000036s) BEGIN
I, [2015-12-27T11:04:29.762830 #31282]  INFO -- : (0.000160s) INSERT INTO 
`messages` (`text`) VALUES ('message-1')
I, [2015-12-27T11:04:29.762935 #31282]  INFO -- : (0.000030s) COMMIT
    Import message: message-2
I, [2015-12-27T11:04:29.763082 #31282]  INFO -- : (0.000021s) BEGIN
I, [2015-12-27T11:04:29.763267 #31282]  INFO -- : (0.000045s) INSERT INTO 
`messages` (`text`) VALUES ('message-2')
I, [2015-12-27T11:04:29.763341 #31282]  INFO -- : (0.000029s) COMMIT
    Import message: message-3
I, [2015-12-27T11:04:29.763464 #31282]  INFO -- : (0.000019s) BEGIN
I, [2015-12-27T11:04:29.763620 #31282]  INFO -- : (0.000042s) INSERT INTO 
`messages` (`text`) VALUES ('message-3')
I, [2015-12-27T11:04:29.763691 #31282]  INFO -- : (0.000030s) COMMIT
==> Parse topic 'http://foo.bar/topic2'
    Import message: message-1
I, [2015-12-27T11:04:29.763822 #31282]  INFO -- : (0.000017s) BEGIN
E, [2015-12-27T11:04:29.764058 #31282] ERROR -- : 
SQLite3::ConstraintException: UNIQUE constraint failed: messages.text: 
INSERT INTO `messages` (`text`) VALUES ('message-1')
I, [2015-12-27T11:04:29.764343 #31282]  INFO -- : (0.000077s) ROLLBACK
    Import message: message-2
I, [2015-12-27T11:04:29.764490 #31282]  INFO -- : (0.000020s) BEGIN
E, [2015-12-27T11:04:29.764649 #31282] ERROR -- : 
SQLite3::ConstraintException: UNIQUE constraint failed: messages.text: 
INSERT INTO `messages` (`text`) VALUES ('message-2')
I, [2015-12-27T11:04:29.764855 #31282]  INFO -- : (0.000021s) ROLLBACK
    Import message: message-3
I, [2015-12-27T11:04:29.764973 #31282]  INFO -- : (0.000018s) BEGIN
E, [2015-12-27T11:04:29.765123 #31282] ERROR -- : 
SQLite3::ConstraintException: UNIQUE constraint failed: messages.text: 
INSERT INTO `messages` (`text`) VALUES ('message-3')
I, [2015-12-27T11:04:29.765315 #31282]  INFO -- : (0.000022s) ROLLBACK
I, [2015-12-27T11:04:29.765366 #31282]  INFO -- : (0.003574s) SELECT * FROM 
`topics`
=> #<Sequel::SQLite::Dataset: "SELECT * FROM `topics`">

This example was run using ruby 2.2.3, ruby-sqlite3 1.3.10, and sqlite3 
3.8.11.1, it's possible other versions have different behavior.

In general, it is not safe to send any queries to the database inside a 
Dataset#each block.  You could try switching the Topic.each to Topic.all 
and see if that makes a difference.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to