Answers.Body: <p>It appears that the foreign key mentioned in the error message doesn't exist for some reason. I would probably go into MySQL and check.  If it's not there, just create one with the same name on the <code>civicrm_case</code> table. Then try the upgrade again.</p>
  
  <p>A MySQL statement to create the foreign key (from CiviCRM 4.5) is: <code>ADD CONSTRAINT FK_civicrm_case_case_type_id FOREIGN KEY (case_type_id) REFERENCES civicrm_case_type (id) ON DELETE SET NULL;</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-21T07:08:56.697Z
Answers.Text: Thank you Dave ; but, before I run this statement, how can I check that this foreign Key does not exist ? Actually, in my civicrm_case table, there is an UNSIGNED "case_type_id" field, commented as being "FK to civicrm_case_type.id" : isn't it already what is required ? (BTW, this table is empty)
Answers.UserDisplayName: Jacques-Marie Moranne
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-21T12:56:59.570Z
Answers.Text: Well, the easiest way would be to run the `add constraint` command.  If it already exists, the add will fail with some message.  No harm done.  I have no idea why the FK doesn't exist, probably a prior upgrade failed somewhere along the way.  If you want to really check for the constraint, here's how: https://stackoverflow.com/questions/4004205/mysql-show-constraints-on-tables-command
Answers.UserDisplayName: DaveD
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-19T14:23:53.143Z
Answers.LastActivityDate: 2017-06-19T14:23:53.143Z
Answers.UserDisplayName: DaveD
Answers.Body: <p>I had the very same problem and I had to fix it by running the following command:</p>
  
  <pre><code>ALTER TABLE civicrm_case
  ADD CONSTRAINT FK_civicrm_case_case_type_id FOREIGN KEY (case_type_id) REFERENCES civicrm_case_type (id) ON DELETE SET NULL;
  </code></pre>
  
  <p>(taken from <code>CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl</code>).</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-28T07:30:23.873Z
Answers.Text: I did it and it worked fine ; thank you to both of you
Answers.UserDisplayName: Jacques-Marie Moranne
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-25T10:52:42.003Z
Answers.LastActivityDate: 2017-06-25T10:52:42.003Z
Answers.UserDisplayName: Onlyjob
Answers.Body: <p>had same issue upgrading from 4.6 to 4.7</p>
  
  <p>running that SQL code in phpmyadmin fixed it.  THANKS!!!!</p>
  
  <p>ALTER TABLE civicrm_case ADD CONSTRAINT FK_civicrm_case_case_type_id FOREIGN KEY (case_type_id) REFERENCES civicrm_case_type (id) ON DELETE SET NULL;</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-26T02:18:52.133Z
Answers.Text: This is not an Answer and should be added as a Comment
Answers.UserDisplayName: petednz - fuzion
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-18T17:27:05.483Z
Answers.Text: Thanks for the info but I need 50 reputation before I have the ability to add a comment to other peoples answers.  I am following the guidelines since I am affirming the solution that worked for me.  your comment and downvote are not helpful to anyone.
Answers.UserDisplayName: Leslie Dugger
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-18T20:12:56.393Z
Answers.Text: Yep, I see that. Apologies. Trying to figure how I can convert your Answer to be a Comment only Onlyjob's Answer - then I can +1 your comment and help you get some reputation.
Answers.UserDisplayName: petednz - fuzion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-22T21:06:54.210Z
Answers.LastActivityDate: 2018-02-22T21:06:54.210Z
Answers.UserDisplayName: Leslie Dugger
Body: <p>I want to upgrade my site (Drupal 7.54 to 7.55, then civicrm 4.7.16 to 4.7.20).</p>
  
  <p>No problem on Drupal upgrade.</p>
  
  <p>But for CiviCRM, when I run mysite/drupal/?q=civicrm/upgrade&amp;reset=1 (after having replaced sites/all/civicrm directory content) :</p>
  
  <ul>
  <li>upgrade to 4.7.17 runs OK</li>
  <li>upgrade to 4.7.18 fails with the following error :</li>
  </ul>
  
  <p><a href="https://i.sstatic.net/N53ca.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/N53ca.png" alt="enter image description here"></a></p>
  
  <p>(BTW, civicrm_case table is empty on my site)</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-22T21:06:54.210Z
Comments.Text: had same issue upgrading from 4.6 to 4.7 running that SQL code in phpmyadmin fixed it. THANKS!!!! ALTER TABLE civicrm_case ADD CONSTRAINT FK_civicrm_case_case_type_id FOREIGN KEY (case_type_id) REFERENCES civicrm_case_type (id) ON DELETE SET NULL;
Comments.UserDisplayName: Leslie Dugger
Tags: <upgrade>
Title: Error when upgrading from 4.7.16 to 4.7.20
ThreadText: # Error when upgrading from 4.7.16 to 4.7.20
  
  (Asked by: username_0 on 2017-06-19)
  
  I want to upgrade my site (Drupal 7.54 to 7.55, then civicrm 4.7.16 to 4.7.20).
  
  No problem on Drupal upgrade.
  
  But for CiviCRM, when I run mysite/drupal/?q=civicrm/upgrade&reset=1 (after having replaced sites/all/civicrm directory content) :
  
  * upgrade to 4.7.17 runs OK
  * upgrade to 4.7.18 fails with the following error :
  
  [![enter image description here](https://i.sstatic.net/N53ca.png)](https://i.sstatic.net/N53ca.png)
  
  (BTW, civicrm\_case table is empty on my site)
  
  **Tags:** upgrade
  
  > **Comments:**
  > **username_1**: had same issue upgrading from 4.6 to 4.7 running that SQL code in phpmyadmin fixed it. THANKS!!!! ALTER TABLE civicrm_case ADD CONSTRAINT FK_civicrm_case_case_type_id FOREIGN KEY (case_type_id) REFERENCES civicrm_case_type (id) ON DELETE SET NULL;  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_2 on 2017-06-19. Score: 1 | Accepted answer
  
  It appears that the foreign key mentioned in the error message doesn't exist for some reason. I would probably go into MySQL and check. If it's not there, just create one with the same name on the `civicrm_case` table. Then try the upgrade again.
  
  A MySQL statement to create the foreign key (from CiviCRM 4.5) is: `ADD CONSTRAINT FK_civicrm_case_case_type_id FOREIGN KEY (case_type_id) REFERENCES civicrm_case_type (id) ON DELETE SET NULL;`
  
  > **Comments:**
  > **username_0**: Thank you Dave ; but, before I run this statement, how can I check that this foreign Key does not exist ? Actually, in my civicrm_case table, there is an UNSIGNED "case_type_id" field, commented as being "FK to civicrm_case_type.id" : isn't it already what is required ? (BTW, this table is empty)  
  > **username_2**: Well, the easiest way would be to run the `add constraint` command.  If it already exists, the add will fail with some message.  No harm done.  I have no idea why the FK doesn't exist, probably a prior upgrade failed somewhere along the way.  If you want to really check for the constraint, here's how: https://stackoverflow.com/questions/4004205/mysql-show-constraints-on-tables-command  
  
  ---
  
  ### Answer by username_3 on 2017-06-25. Score: 0
  
  I had the very same problem and I had to fix it by running the following command:
  
  ```
  ALTER TABLE civicrm_case
  ADD CONSTRAINT FK_civicrm_case_case_type_id FOREIGN KEY (case_type_id) REFERENCES civicrm_case_type (id) ON DELETE SET NULL;
  
  ```
  
  (taken from `CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl`).
  
  > **Comments:**
  > **username_0**: I did it and it worked fine ; thank you to both of you  
  
  ---
  
  ### Answer by username_1 on 2018-02-22. Score: -1
  
  had same issue upgrading from 4.6 to 4.7
  
  running that SQL code in phpmyadmin fixed it. THANKS!!!!
  
  ALTER TABLE civicrm\_case ADD CONSTRAINT FK\_civicrm\_case\_case\_type\_id FOREIGN KEY (case\_type\_id) REFERENCES civicrm\_case\_type (id) ON DELETE SET NULL;
  
  > **Comments:**
  > **username_4**: This is not an Answer and should be added as a Comment  
  > **username_1**: Thanks for the info but I need 50 reputation before I have the ability to add a comment to other peoples answers.  I am following the guidelines since I am affirming the solution that worked for me.  your comment and downvote are not helpful to anyone.  
  > **username_4**: Yep, I see that. Apologies. Trying to figure how I can convert your Answer to be a Comment only username_3's Answer - then I can +1 your comment and help you get some reputation.  
  
  ---
  
  