# Issue after 8.5.x update towards 9.x - Foreign key constraint is incorrectly formed

**URL:** <https://forums.concretecms.org/t/issue-after-8-5-x-update-towards-9-x-foreign-key-constraint-is-incorrectly-formed/7438>\
**Category:** Installation\
**Created:** [May 18, 2024, 12:38pm UTC](https://forums.concretecms.org/t/issue-after-8-5-x-update-towards-9-x-foreign-key-constraint-is-incorrectly-formed/7438 "2024-05-18T12:38:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramonleenders](https://forums.concretecms.org/user_avatar/forums.concretecms.org/ramonleenders/32/3986_2.png) [@ramonleenders](https://forums.concretecms.org/u/ramonleenders)\
**Post date:** [May 18, 2024, 12:38pm UTC](https://forums.concretecms.org/t/issue-after-8-5-x-update-towards-9-x-foreign-key-constraint-is-incorrectly-formed/7438/1 "2024-05-18T12:38:20Z")

</div>

Updating an installation from 8.5.x towards 9.0.2 and then towards the latest 9.3.1. Unfortunately, getting database issues. Not sure what’s causing this. Anyone able to point me in the right direction here?

An exception occurred while executing ‘ALTER TABLE OAuth2ClientScopes ADD CONSTRAINT FK\_497E26F39ECFF1A8 FOREIGN KEY (clientIdentifier) REFERENCES OAuth2Client (identifier) ON DELETE CASCADE’: SQLSTATE[HY000]: General error: 1005 Can’t create table `databasename`.`oauth2clientscopes` (errno: 150 “Foreign key constraint is incorrectly formed”)

Previous exceptions

- SQLSTATE[HY000]: General error: 1005 Can’t create table `databasename`.`oauth2clientscopes` (errno: 150 “Foreign key constraint is incorrectly formed”) (HY000)
- SQLSTATE[HY000]: General error: 1005 Can’t create table `databasename`.`oauth2clientscopes` (errno: 150 “Foreign key constraint is incorrectly formed”) (HY000)

---

<div class="post-metadata">

**Author:** ![JohntheFish](https://forums.concretecms.org/user_avatar/forums.concretecms.org/johnthefish/32/3767_2.png) [@JohntheFish](https://forums.concretecms.org/u/JohntheFish)\
**Post date:** [May 18, 2024, 5:43pm UTC](https://forums.concretecms.org/t/issue-after-8-5-x-update-towards-9-x-foreign-key-constraint-is-incorrectly-formed/7438/2 "2024-05-18T17:43:55Z")

</div>

Its a chicken and egg problem with dependencies in the oAuth tables (you can get similar with express), usually when updating the character set.

If the rest of the update works from the CLI, and you are not using oAuth, you can delete the tables and import them from a fresh 9.x install. Or @jero posted a script to fix from MySQL.

> [@Error upgrading 8.5.16 to 9.2.8](https://forums.concretecms.org/t/error-upgrading-8-5-16-to-9-2-8/7315):
>
> I’m using the elemental theme. I’ve replaced /concrete with the same folder from 9.2.8. I visit the site and get the following error: An exception occurred while executing ‘ALTER TABLE OAuth2ClientScopes ADD CONSTRAINT FK\_497E26F3C7FD45E9 FOREIGN KEY (scopeIdentifier) REFERENCES OAuth2Scope (identifier)’: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint I’ve checked the collation on both tables and they match.

---

<div class="post-metadata">

**Author:** ![ramonleenders](https://forums.concretecms.org/user_avatar/forums.concretecms.org/ramonleenders/32/3986_2.png) [@ramonleenders](https://forums.concretecms.org/u/ramonleenders)\
**Post date:** [May 19, 2024, 9:01am UTC](https://forums.concretecms.org/t/issue-after-8-5-x-update-towards-9-x-foreign-key-constraint-is-incorrectly-formed/7438/3 "2024-05-19T09:01:25Z")

</div>

Indeed deleting ALL of the tables and making an export from another install does the trick. Rather strange workaround, shouldn’t this be fixed somehow in the update/upgrade progress or did I do something wrong or…

---

<div class="post-metadata">

**Author:** ![JohntheFish](https://forums.concretecms.org/user_avatar/forums.concretecms.org/johnthefish/32/3767_2.png) [@JohntheFish](https://forums.concretecms.org/u/JohntheFish)\
**Post date:** [May 19, 2024, 1:02pm UTC](https://forums.concretecms.org/t/issue-after-8-5-x-update-towards-9-x-foreign-key-constraint-is-incorrectly-formed/7438/4 "2024-05-19T13:02:05Z")

</div>

Such problems during updates with interdependent tables has been a long standing problem. Its nothing new. Disabling foreign key checks in migrations doesn’t always resolve it. Fortunately not many sites use oAuth. With Express, not many sites use complex associations between objects, so again it doesn’t crop up so often.

---

<div class="post-metadata">

**Author:** ![mlocati](https://forums.concretecms.org/user_avatar/forums.concretecms.org/mlocati/32/6208_2.png) [@mlocati](https://forums.concretecms.org/u/mlocati)\
**Post date:** [May 19, 2024, 6:09pm UTC](https://forums.concretecms.org/t/issue-after-8-5-x-update-towards-9-x-foreign-key-constraint-is-incorrectly-formed/7438/5 "2024-05-19T18:09:05Z")

</div>

The problem is probability caused by tables having different collations.

This situation may occur when some tables are created using the collation defined in Concrete, and other tables are created using the mysql dedault collation (foreign keys with columns having different collations often break mysql with the error reported).

To solve the situation of having different collations, the temporary solution is so reset all the tables collations (using the `c5:database:charset:set` CLI command or the related dashboard page): that way, all the tables will have the same collation.

The definitive solution would be to find when and how some tables are created using the mysql default collation instead of using the collation defined in Concrete.

---

<div class="post-metadata">

**Author:** ![ramonleenders](https://forums.concretecms.org/user_avatar/forums.concretecms.org/ramonleenders/32/3986_2.png) [@ramonleenders](https://forums.concretecms.org/u/ramonleenders)\
**Post date:** [May 24, 2024, 8:10am UTC](https://forums.concretecms.org/t/issue-after-8-5-x-update-towards-9-x-foreign-key-constraint-is-incorrectly-formed/7438/6 "2024-05-24T08:10:59Z")

</div>

OK, then running in to the next issue here. This error comes when I move from PHP 7.4 to PHP 8.2:

Doctrine \ Common \ Annotations \ AnnotationException  
[Semantical Error] The annotation “@ORM\Entity” in class Concrete\Core\Entity\Site\SiteTree was never imported. Did you maybe forget to add a “use” statement for this annotation?

Any clue to this? I am running other clean installs of 9.3.1 (which is the same as this site), which runs fine. So I’m assuming it’s some generated file that’s gone wrong?

---

<div class="post-metadata">

**Author:** ![ramonleenders](https://forums.concretecms.org/user_avatar/forums.concretecms.org/ramonleenders/32/3986_2.png) [@ramonleenders](https://forums.concretecms.org/u/ramonleenders)\
**Post date:** [May 24, 2024, 8:21am UTC](https://forums.concretecms.org/t/issue-after-8-5-x-update-towards-9-x-foreign-key-constraint-is-incorrectly-formed/7438/7 "2024-05-24T08:21:26Z")

</div>

> [@ramonleenders](#):
>
> Concrete\Core\

After setting doctrine development mode to true (/dashboard/system/environment/entities), it seems to work… Refreshing did nothing. Kind of weird. Never really used this setting myself. I inherited this website and was tasked to update. Not sure how this breaks the whole ORM though… Any thoughts on this?

---

<div class="post-metadata">

**Author:** ![Myq](https://forums.concretecms.org/user_avatar/forums.concretecms.org/myq/32/4281_2.png) [@Myq](https://forums.concretecms.org/u/Myq)\
**Post date:** [May 24, 2024, 8:55am UTC](https://forums.concretecms.org/t/issue-after-8-5-x-update-towards-9-x-foreign-key-constraint-is-incorrectly-formed/7438/8 "2024-05-24T08:55:09Z")

</div>

try regenerating the proxies: `concrete/bin/concrete orm:generate-proxies`

---

<div class="post-metadata">

**Author:** ![ramonleenders](https://forums.concretecms.org/user_avatar/forums.concretecms.org/ramonleenders/32/3986_2.png) [@ramonleenders](https://forums.concretecms.org/u/ramonleenders)\
**Post date:** [May 24, 2024, 9:16am UTC](https://forums.concretecms.org/t/issue-after-8-5-x-update-towards-9-x-foreign-key-constraint-is-incorrectly-formed/7438/9 "2024-05-24T09:16:49Z")

</div>

Unfortunately, like mentioned above, refreshing does not do the trick here. It’s giving me the same issues. Refreshing says

Proxy classes generated to “/application/config/doctrine/proxies”

In the CLI, but after that, it poops out the same error upon loading the site/dashboard. So at this moment, I’m stuck on having development mode set to on… More thoughts? 🙂

---

<div class="post-metadata">

**Author:** ![JohntheFish](https://forums.concretecms.org/user_avatar/forums.concretecms.org/johnthefish/32/3767_2.png) [@JohntheFish](https://forums.concretecms.org/u/JohntheFish)\
**Post date:** [May 24, 2024, 9:26am UTC](https://forums.concretecms.org/t/issue-after-8-5-x-update-towards-9-x-foreign-key-constraint-is-incorrectly-formed/7438/10 "2024-05-24T09:26:21Z")

</div>

I usually run updates with development mode on, then after testing the update switch development mode off and all is well again.

---

<div class="post-metadata">

**Author:** ![Myq](https://forums.concretecms.org/user_avatar/forums.concretecms.org/myq/32/4281_2.png) [@Myq](https://forums.concretecms.org/u/Myq)\
**Post date:** [May 28, 2024, 8:24am UTC](https://forums.concretecms.org/t/issue-after-8-5-x-update-towards-9-x-foreign-key-constraint-is-incorrectly-formed/7438/11 "2024-05-28T08:24:09Z")

</div>

@ramonleenders

> [@ramonleenders](#):
>
> The annotation “@ORM\Entity” in class Concrete\Core\Entity\Site\SiteTree was never imported

My brain wasn’t working well on Friday afternoon! I’ve not only seen this before, I actually submitted a PR to fix it last year. The problem is described here: [Unable to install database: [Semantical Error] · Issue #46 · concretecms/composer · GitHub](https://github.com/concretecms/composer/issues/46)

And the thing that fixed it for me was to keep Doctrine \<2.14: [keep doctrine/orm below 2.14 by wordish · Pull Request #45 · concretecms/composer · GitHub](https://github.com/concretecms/composer/pull/45/files)
