I have generated this migration in symfony, to add createdAt and UpdatedAt fields to the table
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241105173344 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE user ADD created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', ADD updated_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\'');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE `user` DROP created_at, DROP updated_at');
}
}
but when I try to execute the migration
An exception occurred while executing a query: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'user' already exists
The migration is not trying to create the table again , I don't know what is wrong on the migration
Doctrine migrations system save all executed migrations in a separate table, to "remember" which migrations have already been executed. Once you execute the migration process, it finds first not executed migration in the code and executes all not executed migration, starting from this one.
Now, getting back to your error. It clearly says Table 'user' already exists
, which means it's trying to create the table. It may happen if for some reason you already have this table in your DB, but the migration responsible for this change is not marked as executed, and when you're starting the migration process, it tries to execute "create table user" query, which is most likely somewhere in your old migrations in the code. It has nothing to do with the migration you gave us in the question.