2014-10-01 95 views
0

我真的是新手在這個東西。事情是...我有文章網站。人們可以評價那裏的文章。如果沒有人評價,我可以刪除文章。但如果有人額定文章中,我不斷收到以下錯誤:外鍵違規:7錯誤

PDOException: SQLSTATE[23503]: Foreign key violation: 7 ERROR: update or delete on table "article" violates foreign key constraint "article_rating_item_id_fkey" on table "article_ratings" 
DETAIL: Key (id)=(xxxx) is still referenced from table "article_ratings". in /libs/Nette/loader.php:3515 @ http://www.xxxxxx/admin/articleedit/3578?do=deletearticle @@ exception-2014-09-29-18-14-37-b625334b3e569cb7661f1704256874c1.htm 

當我檢查該文件中,有跟隨着代碼:

public function handleDeletearticle($id) 
     { 
      $article = $this->context->createArticles()->get($id); 
      $this->context->createArticles()->where("id", $id)->delete(); 
      $this->flashMessage('Done', 'success'); 
      $this->redirect('Admin:articles'); 
     } 

能否請你幫我如何解決它?謝謝你在前進

編輯:這是它的外觀Articles.php

public function selectArticleWithRating($slug) 
{ 
    $article = $this->query("Select article.*, COUNT(rating.id) AS plus, COUNT(rating2.id) AS minus, \"user\".avatar, \"user\".username 
    FROM article 
    LEFT JOIN rating AS rating ON rating.item_id=article.id and rating.type='article' and rating.rate=1 
    LEFT JOIN rating AS rating2 ON rating2.item_id=article.id and rating2.type='article' and rating2.rate=0 
    LEFT JOIN \"user\" ON \"user\".id=article.user_id 
    WHERE slug='$slug' 
    GROUP BY article.id, \"user\".id"); 

    return $article; 
} 

不應該有article_ratings

+3

您需要首先刪除'article_ratings'中的通訊記錄行,因爲您有'外部文章'的外鍵,它*具有*來引用'article'。 – h2ooooooo 2014-10-01 13:06:41

+0

謝謝。有辦法如何做到這一點在文件中,或者我必須在數據庫中做到這一點? – Charmed 2014-10-01 13:10:20

+0

我不知道你的數據庫類,但我相信你可以做到這一點。查詢是'DELETE FROM table WHERE column = value'。 – h2ooooooo 2014-10-01 13:11:07

回答

1

它真的在你得到的錯誤信息中這麼說,你有一個外鍵引用衝突。這意味着您要刪除其數據庫中的某處所引用的行,它甚至會告訴你在哪裏:

is still referenced from table "article_ratings" 

您可以通過使用ON DELETE CASCADE http://www.mysqltutorial.org/mysql-on-delete-cascade/

有一個刪除闖民宅行以及問題的SO覆蓋此位置:MySQL on delete cascade. Test Example

而且很好的解釋在這裏:https://dba.stackexchange.com/questions/44956/good-explanation-of-cascade-on-delete-update-behavior

編輯:在Postgres的:

CREATE TABLE order_items (
    product_no integer REFERENCES products ON DELETE RESTRICT, 
    order_id integer REFERENCES orders ON DELETE CASCADE, 
    quantity integer, 
    PRIMARY KEY (product_no, order_id) 
); 

http://www.postgresql.org/docs/9.3/static/ddl-constraints.html

+0

謝謝。即使對於postgresql它會工作嗎? – Charmed 2014-10-01 13:22:11

+0

看起來像這樣:http://www.postgresql.org/docs/9.3/static/ddl-constraints.html – 2014-10-01 13:23:39

+0

謝謝。我道歉。我真的很愚蠢。我無法訪問數據庫。僅通過FTP訪問文件。有沒有選擇呢? – Charmed 2014-10-01 16:52:00

0

作爲另一種選擇通過@hebron依賴於改變外鍵級聯刪除行爲給出了答案,你可能會發現你的代碼中更簡單易懂(即不依賴於「隱藏的」數據庫行爲)在連接中刪除。

DELETE articles, article_ratings 
FROM articles 
LEFT JOIN article_ratings 
    ON articles.id = article_ratings.article_id /* or whatever your foreign key name is */ 
WHERE articles.id = ? 
+0

謝謝。這會有幫助嗎? '公共職能selectArticleWithRating($蛞蝓) \t { \t \t $文章= $這個 - >查詢(「選擇的文章。*,COUNT(rating.id)爲正,COUNT(rating2.id)爲負,\ 「用戶\」。化身,\ 「用戶\」。用戶名 \t \t FROM文章 \t \t LEFT JOIN評級AS評級ON rating.item_id = article.id和rating.type = '項目' 和rating.hodnoceni = 1 \t \t LEFT JOIN評分AS rating2 ON rating2.item_id = article.id和rating2.type ='article'and rating2.hodnoceni = 0 \t \t LEFT JOIN \「user \」ON \「use r \「.id = article.user_id \t \t WHERE slug ='$ slug' \t \t GROUP BY article.id,\」user \「.id」); \t \t return $ article; \t}' – Charmed 2014-10-01 15:59:19

+0

@Charmed會幫助什麼? – 2014-10-01 17:24:20

+0

我的意思是......如果我可以將您的代碼插入到我在下面發佈的代碼中? – Charmed 2014-10-01 18:34:45