Mysql 数据映射器可以';由于关系,无法删除记录
我使用Torrent和Tag设置了很多DataMapper/MySQL,如下所示:Mysql 数据映射器可以';由于关系,无法删除记录,mysql,ruby,orm,relational-database,datamapper,Mysql,Ruby,Orm,Relational Database,Datamapper,我使用Torrent和Tag设置了很多DataMapper/MySQL,如下所示: class Torrent include DataMapper::Resource property :id, Serial property :name, String property :magnet, Text property :created_at, DateTime has n, :tags, :through => Re
class Torrent
include DataMapper::Resource
property :id, Serial
property :name, String
property :magnet, Text
property :created_at, DateTime
has n, :tags, :through => Resource
end
class Tag
include DataMapper::Resource
property :id, Serial
property :name, String
property :hits, Integer
has n, :torrents, :through => Resource
end
但是,当试图通过torrent.first.destroy
或类似方式销毁一个torrent时,DataMapper返回false
我尝试了直接的SQL查询,比如从名为“%ubuntu%”的torrents中删除,
,但由于MySQL错误1451而失败:
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`brightswipe`.`tag_torrents`, CONSTRAINT `tag_torrents_torrent_fk` FOREIGN KEY (`torrent_id`) REFERENCES `torrents` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
我认为有一些DataMapper设置,在删除torrent时,我可以:
我如何才能做到这一点?尝试使用此插件自动管理关系: 这将允许您销毁M:M assocs,尽管您必须手动清理assocs表:
class Tag
...
has n, :torrents, :through => Resource, :constraint => :skip
class Torrent
...
has n, :tags, :through => Resource, :constraint => :skip
另一个选项是手动从assocs表中删除关系,然后您可以删除没有任何问题的项,因为您通过从assocs表中删除相应的条目来销毁关系
基本示例:
tr = Torrent.create
tg = Tag.create
tr.tags << tg
tr.save
tg.torrents << tr
tg.save
# destroying relation
TagTorrent.first(:tag => tg, :torrent => tr).destroy!
# or
tr.tag_torrents(:tag => tg).destroy
# or
tg.tag_torrents(:torrent => tr).destroy
# destroy items
tr.destroy!
tg.destroy!
tr=Torrent.create
tg=Tag.create
摧毁!
#或
tr.tag_torrents(:tag=>tg)。销毁
#或
tg.tag_torrents(:torrent=>tr).destroy
#销毁物品
摧毁!
毁灭!
尝试使用此插件自动管理关系:
这将允许您销毁M:M assocs,尽管您必须手动清理assocs表:
class Tag
...
has n, :torrents, :through => Resource, :constraint => :skip
class Torrent
...
has n, :tags, :through => Resource, :constraint => :skip
另一个选项是手动从assocs表中删除关系,然后您可以删除没有任何问题的项,因为您通过从assocs表中删除相应的条目来销毁关系
基本示例:
tr = Torrent.create
tg = Tag.create
tr.tags << tg
tr.save
tg.torrents << tr
tg.save
# destroying relation
TagTorrent.first(:tag => tg, :torrent => tr).destroy!
# or
tr.tag_torrents(:tag => tg).destroy
# or
tg.tag_torrents(:torrent => tr).destroy
# destroy items
tr.destroy!
tg.destroy!
tr=Torrent.create
tg=Tag.create
摧毁!
#或
tr.tag_torrents(:tag=>tg)。销毁
#或
tg.tag_torrents(:torrent=>tr).destroy
#销毁物品
摧毁!
毁灭!
删除torrents后会删除标记关联吗?在标记删除时删除标记关联?而且,“constraint=>set_nil”事件会引发一个ArgumentError;它对于多个关系无效。请尝试:constraint=>:skip
then@silvu请解释它的作用。@silvu-这将删除关系而不是删除其他关系?我更希望当标记被删除时,它们会从关联表中删除,反之亦然,当torrents被删除时,会在删除torrents时删除标记关联吗?在标记删除时删除标记关联?而且,“constraint=>set_nil”事件会引发一个ArgumentError;它对于多个关系无效。请尝试:constraint=>:skip
then@silvu请解释它的作用。@silvu-这将删除关系而不是删除其他关系?我希望当标签被删除时,它们会从关联表中删除,反之亦然,当torrents被删除时