Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 数据映射器可以';由于关系,无法删除记录_Mysql_Ruby_Orm_Relational Database_Datamapper - Fatal编程技术网

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

我使用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 => 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时,我可以:

  • 删除标记关联
  • 删除激流
  • 删除标签时,我可以:

  • 从具有该标记的所有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被删除时