DEV Community

Cover image for Resolvendo problema de "foreign key constraint" ao migrar o Ghost CMS da v4 para v5
Marcelo Albuquerque
Marcelo Albuquerque

Posted on

Resolvendo problema de "foreign key constraint" ao migrar o Ghost CMS da v4 para v5

Ao atualizarmos o Ghost para sua versão 5, diversos scripts de migração são executados, incluindo scripts que realizam alterações no Banco de Dados. E é justamente um desses scripts que ao não conseguir realizar uma mudança na base de dados, impossibilita a execução do Ghost.

Esse script tenta realizar uma mudança que afeta uma tabela com foreign key constraint , o que é bloqueado pelo Banco de Dados.

O script em questão, tenta executar o seguinte:

alter table `subscriptions` modify  `tier_id` varchar(24) not null
Enter fullscreen mode Exit fullscreen mode

Podemos observar que ele tenta executar uma mudança na tabela subscriptions e nos retorna o seguinte erro:

Message: Ghost was able to start, but errored during boot with: alter table `subscriptions` modify  `tier_id` varchar(24) not null  - Cannot change column 'tier_id': used in a foreign key constraint 'subscriptions_tier_id_foreign'
Context: [object Object]
Help: Error occurred while executing the following migration: 2022-10-18-05-39-drop-nullable-tier-id.js
Enter fullscreen mode Exit fullscreen mode

Para solucionar esse problema devemos desabilitar a checagem foreign keys antes de executar o script, após a execução da query podemos reabilitar a checagem normalmente. O script a seguir deve ser executado diretamente na base de dados de forma manual:

USE ghost_test; # Aqui seleciono a base de dados onde a tabela se encontra
SET foreign_key_checks = 0; # Desabilito as checagens foreign keys
alter table `subscriptions` modify  `tier_id` varchar(24) not null; # Executo a query que o Ghost tentou realizar
SET foreign_key_checks = 1; # Reabilito as checagens foreign keys
Enter fullscreen mode Exit fullscreen mode

Agora, de volta ao Ghost, podemos executar o comando para iniciar sua execução:

ghost start
Enter fullscreen mode Exit fullscreen mode

Processo finalizado!

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay