# Migration error

**URL:** https://forum.mautic.org/t/migration-error/26979
**Category:** Mautic 4 Install/Upgrade Support
**Created:** [February 8, 2023, 3:32pm UTC](https://forum.mautic.org/t/migration-error/26979 "2023-02-08T15:32:03Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)
#### Post date: [February 8, 2023, 3:32pm UTC](https://forum.mautic.org/t/migration-error/26979/1 "2023-02-08T15:32:04Z")

</div>

**Your software**  
_My PHP version is_ : 7.4, 8.0  
_My MySQL/MariaDB version is_ (delete as applicable): MySQL/MariaDB version 10.5

**Updating/Installing Errors**  
_I am_ (delete as applicable): Updating  
_Upgrading/installing via_ (delete as applicable) : Command Line

_These errors are showing in the installer_ :  
The problem seems to be with foreign keys need to be dropped

_These errors are showing in the Mautic log_ :

**Your problem**  
After completing migrations successfully I get the following error:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/2X/4/4696dd7f7fa71cfc9da4c746ad4599a64bebe8c1.png)

```auto
In AbstractMySQLDriver.php line 128:
                                                                                                                         
  An exception occurred while executing 'DROP INDEX IDX_1AE3441319EB6921 ON oauth2_user_client_xref':                    
  SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP INDEX `IDX_1AE3441319EB6921`; check that it exists  
In Exception.php line 18:
  SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP INDEX `IDX_1AE3441319EB6921`; check that it exists  
In PDOConnection.php line 141:
  SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP INDEX `IDX_1AE3441319EB6921`; check that it exists

```

There is no way I can move pass this.  
The indexes don’t exist, and this prohibits me to do proper upgrades.

The culprit seems to be this PR maybe?

> <https://github.com/mautic/mautic/commit/fcc1d2be66b7dc7e30fcb439ef4991f329f8b21d#diff-b50e24c8ed3c1b015020af4d15c31b0955de2bdb7c3f2dca9175ef2f4ce1c88e>
>
> phpstan fixes
> 
> Revert "phpstan fixes"
> 
> Update app/bundles/CoreBundle/EventLi…stener/DoctrineEventsSubscriber.php
> 
> Co-authored-by: John Linhart \<jan@linhart.email\>
> 
> Co-authored-by: John Linhart \<jan@linhart.email\>
> Co-authored-by: John Linhart \<admin@escope.cz\>

So if you go in this file  
app/bundles/CoreBundle/EventListener/DoctrineEventsSubscriber.php  
and you make this change on line 138

 ![image](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/2X/d/d5f525af8fce3b968b436d6ce2552f25e6a45873.png)

All the migrations will be fine.  
Here is the question: is this because the bug is fixed, or it’s a workaround, that should not be applied?  
If this doesn’t fix it, then what would be?

PS: this fix is for sure wrong, but maybe inspires someone to point me in the right direction.

Thanks.  
Joey

---

<div class="post-metadata">

### Author: ![silavapi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/silavapi/32/7424_2.png) [@silavapi](https://forum.mautic.org/u/silavapi)
#### Post date: [February 8, 2023, 4:30pm UTC](https://forum.mautic.org/t/migration-error/26979/2 "2023-02-08T16:30:30Z")

</div>

@mollux can you help with this so that @joeyk still has some hair left next week? 🤣

---

<div class="post-metadata">

### Author: ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)
#### Post date: [February 16, 2023, 12:52pm UTC](https://forum.mautic.org/t/migration-error/26979/3 "2023-02-16T12:52:56Z")

</div>

Some addition to this issue:

1. No migrations left.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/2X/0/0fc602695bceea40effb997f76986953b30a7754.png)

1. Running the following command gives me the follwing result:

`bin/console doctrine:schema:update --dump-sql`

```auto
 * ALTER TABLE oauth2_accesstokens CHANGE client_id client_id INT UNSIGNED NOT NULL, CHANGE user_id user_id INT UNSIGNED DEFAULT NULL, CHANGE token token VARCHAR(191) NOT NULL, CHANGE scope scope VARCHAR(191) DEFAULT NULL
 * ALTER TABLE oauth2_refreshtokens CHANGE client_id client_id INT UNSIGNED NOT NULL, CHANGE user_id user_id INT UNSIGNED NOT NULL, CHANGE token token VARCHAR(191) NOT NULL, CHANGE scope scope VARCHAR(191) DEFAULT NULL
 * ALTER TABLE oauth2_clients CHANGE id id INT UNSIGNED AUTO_INCREMENT NOT NULL, CHANGE name name VARCHAR(191) NOT NULL, CHANGE random_id random_id VARCHAR(191) NOT NULL, CHANGE secret secret VARCHAR(191) NOT NULL
 * ALTER TABLE oauth2_clients ADD CONSTRAINT FK_F9D02AE6D60322AC FOREIGN KEY (role_id) REFERENCES roles (id)

```

– AND FURTHER 100 lines…

But running

 ![image](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/2X/9/9cc754d03b5f87d13ad99ab61c218af13b97680a.png)

Either this error, or “No key found” error.

Thx for checking!

---

<div class="post-metadata">

### Author: ![Yosu\_Cadilla](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/yosu_cadilla/32/6206_2.png) [@Yosu\_Cadilla](https://forum.mautic.org/u/Yosu_Cadilla)
#### Post date: [February 16, 2023, 6:32pm UTC](https://forum.mautic.org/t/migration-error/26979/4 "2023-02-16T18:32:30Z")

</div>

I guess you could make a snapshot, remove the offending keys, do the migration, check consistency, add the keys back (if needed).

Sounds very familiar, similar errors were found when migrating from early M3 versions to late M3 versions for Mautic instances previously migrated from M2 to M3 due to a problem in the M2 to M3 migration code that was dormant until you needed to upgrade to a newer M3 version.

Could this still be related?

> <https://github.com/mautic/mautic/issues/9709>
>
> \[//\]: # ( Invisible comment: 
> IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII…I
> Before you create the issue:
> IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
> Search for similar report among other reported issues.
> Learn how to troubleshoot at https://www.mautic.org/docs/en/tips/troubleshooting.html
> Use drag&drop to attach images or other files )
> 
> \## Bug Description
> 
> When upgrading to 3.3 from 3.2.5 the migrations did not run correctly. The error was 
> 
> \`\`\`
> ALTER TABLE lead\_lists ADD category\_id INT UNSIGNED DEFAULT NULL -\> ALTER TABLE lead\_lists ADD CONSTRAINT FK\_6EC1522A12469DE2 FOREIGN KEY (category\_id) REFERENCES categor
> ies (id) ON DELETE SET NULL Migration 20210104171005 failed during Execution. Error An exception occurred while executing 'ALTER TABLE lead\_lists ADD CONSTRAINT FK\_6EC1522A12469DE2 FOREIGN K
> EY (category\_id) REFERENCES categories (id) ON DELETE SET NULL': \\ SQLSTATE\[HY000\]: General error: 1005 Can't create table \`#sql-965\_c385\` (errno: 150 "Foreign key constraint is incorrectly formed") 
> \`\`\`
> 
> The isse is, that in migration https://github.com/mautic/mautic/blob/3.3.0/app/migrations/Version20210104171005.php is run command 
> 
> \`\`\`
> ALTER TABLE {$this-\>prefix}lead\_lists ADD category\_id INT UNSIGNED DEFAULT NULL
> \`\`\`
> 
> Without explicitly set width of the column, in our case it created INT(10) and then the migration failed. After setting this field to INT(11) ran without an error.  
> 
> Safer to have 
> 
> \`\`\`
> ALTER TABLE {$this-\>prefix}lead\_lists ADD category\_id INT(11) UNSIGNED DEFAULT NULL
> \`\`\`
> 
> Probably the cause is the setting and version of Mysql server (5.5.5-10.2.16-MariaDB-10.2.16+maria~artful). 
> 
> Thanks
> 
> | Q | A
> | --- | ---
> | Mautic version | 3.2.5
> | PHP version | 7.4
> | Mysql version | 5.5.5-10.2.16-MariaDB-10.2.16+maria~artful
> | Browser | any
> 
> \### Steps to reproduce
> 1. Upgrade to 3.3.0 
> 
>  
> \### Log errors
> 
> ALTER TABLE lead\_lists ADD category\_id INT UNSIGNED DEFAULT NULL -\> ALTER TABLE lead\_lists ADD CONSTRAINT FK\_6EC1522A12469DE2 FOREIGN KEY (category\_id) REFERENCES categor
> ies (id) ON DELETE SET NULL Migration 20210104171005 failed during Execution. Error An exception occurred while executing 'ALTER TABLE lead\_lists ADD CONSTRAINT FK\_6EC1522A12469DE2 FOREIGN K
> EY (category\_id) REFERENCES categories (id) ON DELETE SET NULL': \\ SQLSTATE\[HY000\]: General error: 1005 Can't create table \`#sql-965\_c385\` (errno: 150 "Foreign key constraint is incorrectly formed") 
> 
> \[//\]: # ( Invisible comment:
> Please check for related errors in the latest log file in \[mautic root\]/app/log/ and/or the web server's logs and post them here. Be sure to remove sensitive information if applicable. )

Similar issues:

> [@MySQL error upgrading to 4.2](https://forum.mautic.org/t/mysql-error-upgrading-to-4-2/22867/20):
>
> Check [Troubleshooting | Mautic](https://docs.mautic.org/en/troubleshooting) and if it’s not related to this thread, please start a new one so we don’t mix several issues together.

> [@Error while upgrading from 3.3.4 to 4.0](https://forum.mautic.org/t/error-while-upgrading-from-3-3-4-to-4-0/20686):
>
> Your software My PHP version is : 7.4 My MySQL/MariaDB version is (delete as applicable): MySQL/MariaDB version 10.3.31-MariaDB-log-cll-lve Updating/Installing Errors I am: Updating Upgrading via : Web These errors are showing in the installer : These errors are showing in the Mautic log : [2021-08-30 18:44:08] console.CRITICAL: Error thrown while running command “doctrine:migrations:migrate --no-interaction --env=prod --no-debug”. Message: “An exception occurred while executing ‘ALTER T…

---

<div class="post-metadata">

### Author: ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)
#### Post date: [February 17, 2023, 11:17am UTC](https://forum.mautic.org/t/migration-error/26979/5 "2023-02-17T11:17:04Z")

</div>

I’ve been working with lot’s of ‘retro’ Mautics lately and helped a bunch of people updating from M2.  
Yes, hacking the DB and manually pushing this update forward is an option, but I wish there would be a fix for this so the migration can be completed with just one command.

---

<div class="post-metadata">

### Author: ![modifiedcontent](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/modifiedcontent/32/511_2.png) [@modifiedcontent](https://forum.mautic.org/u/modifiedcontent)
#### Post date: [February 18, 2023, 2:43pm UTC](https://forum.mautic.org/t/migration-error/26979/6 "2023-02-18T14:43:17Z")

</div>

Isn’t this the [same issue](https://forum.mautic.org/t/as-usual-upgrade-to-4-2-2-this-time-effed-up-my-installation/24106/5) that I and others have reported about last year? Involving oauth2\_accesstokens, foreign keys, indexes, etc.

---

<div class="post-metadata">

### Author: ![Yosu\_Cadilla](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/yosu_cadilla/32/6206_2.png) [@Yosu\_Cadilla](https://forum.mautic.org/u/Yosu_Cadilla)
#### Post date: [February 18, 2023, 4:44pm UTC](https://forum.mautic.org/t/migration-error/26979/7 "2023-02-18T16:44:40Z")

</div>

Yes, another important issue thrown under the carpet, and if you pull the commit thread long enough (m2 to m3 migration), you will find the same person as always…

---

<div class="post-metadata">

### Author: ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)
#### Post date: [February 19, 2023, 7:16am UTC](https://forum.mautic.org/t/migration-error/26979/8 "2023-02-19T07:16:59Z")

</div>

I think that some returning issues are getting in focus from time to time.

If there is enough attention it might be fixed, otherwise the workaround becomes common knowledge, get’s a documentation and life just goes on.

This bug however will cause problems to anyone who wants to update an old instance (from 2, 3).  
Not just once, but even if you managed to push the migrations further, it might come back again, like an untreated sicknes.

It directly affects (effects? 🙂 ) Mautic’s oldest and most loyal users with every upgrade. Not just Mautic upgrade, but I ran into this when I tried to install a plugin and needed to run migrations.

Can someone PLEASE take a look and post here an answer - is this a complicated bug, or really just few small changes. I think there is a logical issue in the code, that makes the migration stuck.

Thank you in advance.

---

<div class="post-metadata">

### Author: ![Yosu\_Cadilla](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/yosu_cadilla/32/6206_2.png) [@Yosu\_Cadilla](https://forum.mautic.org/u/Yosu_Cadilla)
#### Post date: [February 19, 2023, 2:35pm UTC](https://forum.mautic.org/t/migration-error/26979/9 "2023-02-19T14:35:51Z")

</div>

> [@joeyk](#):
>
> It directly affects (effects? 🙂 ) Mautic’s oldest and most loyal users with every upgrade. Not just Mautic upgrade, but I ran into this when I tried to install a plugin and needed to run migrations.

Exactly, it’s like the “coup de grace” for undecided long term users, they’re putting up with the usual issues, they decide to upgrade, and this thing attacks them, which is out of the scope of knowledge for most Mautic users…  
I wonder how many long-term-Mautic-using companies might have been lost to this issue…

What would be the end game here @joeyk? Getting one more release of M4 with this fix? A retroactive patch for M2/M3 migrations?

---

<div class="post-metadata">

### Author: ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)
#### Post date: [February 19, 2023, 4:14pm UTC](https://forum.mautic.org/t/migration-error/26979/10 "2023-02-19T16:14:09Z")

</div>

I think the migration mentioned above has a faulty condition, that could be fixed.

---

<div class="post-metadata">

### Author: ![Yosu\_Cadilla](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/yosu_cadilla/32/6206_2.png) [@Yosu\_Cadilla](https://forum.mautic.org/u/Yosu_Cadilla)
#### Post date: [February 19, 2023, 4:32pm UTC](https://forum.mautic.org/t/migration-error/26979/11 "2023-02-19T16:32:16Z")

</div>

Indeed @joeyk , but how do you think this fix should be delivered to the Mautic users?

Examples:

- A standalone PR? (but most people will have no idea what to do with a PR)
- New migration script(s)? If so for which versions? M2 → M3 or M3–\>M4?
- A new release, like M4.4.7?

---

<div class="post-metadata">

### Author: ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)
#### Post date: [February 20, 2023, 11:41am UTC](https://forum.mautic.org/t/migration-error/26979/12 "2023-02-20T11:41:03Z")

</div>

Great question @Yosu_Cadilla , let’s see what the fix is… If the mighration is not correct, then it should be the migration file, that is fixed. I’ve seen other migration being fixed later, after we discussed it in the forums.

---

<div class="post-metadata">

### Author: ![Yosu\_Cadilla](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/yosu_cadilla/32/6206_2.png) [@Yosu\_Cadilla](https://forum.mautic.org/u/Yosu_Cadilla)
#### Post date: [February 21, 2023, 3:43am UTC](https://forum.mautic.org/t/migration-error/26979/13 "2023-02-21T03:43:26Z")

</div>

That would be a great problem for a proper DBA to solve, and seeing how many unresolved issues seem to be DB related, it would make sense to ask for such to some of the Mautic contributing companies, which might already have such a talent. Or even hire some talent for a few hours as a last resort.

---

<div class="post-metadata">

### Author: ![UHLHosting](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/uhlhosting/32/3043_2.png) [@UHLHosting](https://forum.mautic.org/u/UHLHosting)
#### Post date: [August 19, 2023, 2:43pm UTC](https://forum.mautic.org/t/migration-error/26979/14 "2023-08-19T14:43:41Z")

</div>

Guys for so many years no resolution to this matter? Its a bit too much imho.
