I have a database schema like that
我有一个这样的数据库模式
DataMapper.setup :default, "sqlite://#{Dir.pwd}/image.db"
class Image
include DataMapper::Resource
property :id , Serial
property :url , String,unique: true
property :desc , String,unique: true,default: "empty"
end
DataMapper.finalize.auto_upgrade!
Then i have a loop like this
然后有一个这样的循环。
links.each do |link|
puts link
if Image.all(:url=>link).empty? == true
img=Image.create(:url => link)
puts "Cannot save to database "if img.saved? == false
end
end
I want to add a link when it is not already in a database. I run the script and always get cannot save to dabase. What am I doing wrong? Thanks for any help. Edit: I add to my code
当链接不在数据库中时,我想添加它。我运行脚本,总是无法保存到dabase。我做错了什么?感谢任何帮助。编辑:我添加了我的代码。
DataMapper::Logger.new($stdout, :debug)
and there was no insert queries only selects.
没有插入查询,只有选择。
2 个解决方案
#1
3
You declared :desc property to be unique and set default value to "empty" and in your code example you're creating Image without specifying :desc which results in uniqueness validation failure, that's why your images aren't saved. You can either remove unique option from :desc property declaration or make sure to provide unique value for :desc when creating images.
您声明:desc属性为唯一,并将默认值设置为“empty”,在您的代码示例中,您在创建映像时没有指定:desc,这将导致惟一验证失败,这就是为什么您的映像没有保存。您可以从:desc属性声明中删除惟一选项,或者在创建映像时确保为:desc提供惟一值。
Also, to make your code more verbose so you could see any validation errors try this:
另外,要使代码更加冗长,以便您可以看到任何验证错误,请尝试以下方法:
links.each do |link|
image = Image.new(:url => link)
if image.save
puts "Image saved"
else
puts "Failed to save image: #{image.errors.inspect}"
end
end
Please note that to use validation errors you need to require 'dm-validations' gem.
请注意,要使用验证错误,需要使用“dm验证”gem。
#2
0
What do you mean you get "cannot save to database"?
你说的“无法保存到数据库”是什么意思?
You can try something like this and see if it works a bit better:
你可以试试这样的方法,看看效果是否更好:
links.each do |link|
if Image.first(:url => link)
p "image exists"
else
if Image.create(:url => link)
p "Image succesfully saved"
else
p "could not save image"
end
end
end
#1
3
You declared :desc property to be unique and set default value to "empty" and in your code example you're creating Image without specifying :desc which results in uniqueness validation failure, that's why your images aren't saved. You can either remove unique option from :desc property declaration or make sure to provide unique value for :desc when creating images.
您声明:desc属性为唯一,并将默认值设置为“empty”,在您的代码示例中,您在创建映像时没有指定:desc,这将导致惟一验证失败,这就是为什么您的映像没有保存。您可以从:desc属性声明中删除惟一选项,或者在创建映像时确保为:desc提供惟一值。
Also, to make your code more verbose so you could see any validation errors try this:
另外,要使代码更加冗长,以便您可以看到任何验证错误,请尝试以下方法:
links.each do |link|
image = Image.new(:url => link)
if image.save
puts "Image saved"
else
puts "Failed to save image: #{image.errors.inspect}"
end
end
Please note that to use validation errors you need to require 'dm-validations' gem.
请注意,要使用验证错误,需要使用“dm验证”gem。
#2
0
What do you mean you get "cannot save to database"?
你说的“无法保存到数据库”是什么意思?
You can try something like this and see if it works a bit better:
你可以试试这样的方法,看看效果是否更好:
links.each do |link|
if Image.first(:url => link)
p "image exists"
else
if Image.create(:url => link)
p "Image succesfully saved"
else
p "could not save image"
end
end
end