1

I'm doing a web application using Symfony Framework with MySQL as the database. I'm using doctrine to do the ORM to map the database. I created the schema and did the doctrine:migrations:diff to verify the queries and check that my relations where in order and they appear to be. But when I want to migrate the new schema to MySQL the following error appears: Picture of the Command Line describing the error

Here's the Business Class

/**
* @ORM\Column(type="string")
*/
private $name;

/**
 * @ORM\Column(type="string")
 */
 private $stage_Of_Development;


//Relations

/**
 * @ORM\ManyToOne(targetEntity="Client")
 * @ORM\JoinColumn(name="client_email", referencedColumnName="clientEmail")
 */
private $client;


//Getters and Setters

/**
 * @return mixed
 */
public function getClient()
{
    return $this->client;
}

 /**
 * @param mixed $client
 */
public function setClient(Client $client)
{
    $this->client = $client;
}
}

Here's the Client Class:

<?php
/**
 * @ORM\Entity
 * @ORM\Table(name="client")
 */
class Client
{
/**
 * @ORM\Id
 * @ORM\Column(type="string", unique=true)
 */
private $clientEmail;    

/**
 * @return mixed
 */
public function getClientEmail()
{
    return $this->clientEmail;
}

/**
 * @param mixed $clientEmail
 */
public function setClientEmail($clientEmail)
{
    $this->clientEmail = $clientEmail;
}   
}

Here's the Migration:

namespace Application\Migrations;

class Version20170325232622 extends AbstractMigration
{
 /**
  * @param Schema $schema
  */
 public function up(Schema $schema)
 {
    // this up() migration is auto-generated, please modify it to your needs
    $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

    $this->addSql('ALTER TABLE business ADD client_email VARCHAR(255) DEFAULT NULL');
    $this->addSql('ALTER TABLE business ADD CONSTRAINT FK_8D36E3844FFE0C3 FOREIGN KEY (client_email) REFERENCES client (clientEmail)');
    $this->addSql('CREATE UNIQUE INDEX UNIQ_8D36E38A89DB457 ON business (business_id)');
    $this->addSql('CREATE INDEX IDX_8D36E3844FFE0C3 ON business (client_email)');
}

/**
 * @param Schema $schema
 */
public function down(Schema $schema)
{
    // this down() migration is auto-generated, please modify it to your needs
    $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

    $this->addSql('ALTER TABLE business DROP FOREIGN KEY FK_8D36E3844FFE0C3');
    $this->addSql('DROP INDEX UNIQ_8D36E38A89DB457 ON business');
    $this->addSql('DROP INDEX IDX_8D36E3844FFE0C3 ON business');
    $this->addSql('ALTER TABLE business DROP client_email');
}
}

The schema contains more classes and the client and business entities contain more attributes but for the sake of simplicity I only posted the ones generated the error. I would appreciate anybody's help with this as its my first time using these technologies and I'm kind of lost.

1 Answer 1

4

During the development phase I often have to "clear" previous migrations and then start fresh. You SHOULD NOT DO THIS if your database is in production!! To start fresh, you can drop the database, re-create it, delete previous migrations, then re-run the diff and migrate. In symfony3, I do this:

// WARNING: This will delete your database!!!
>
> bin/console doctrine:database:drop --force
> bin/console doctrine:database:create
> rm -f app/DoctrineMigrations/*
>
> bin/console doctrine:migrations:diff
> bin/console doctrine:migrations:migrate
Sign up to request clarification or add additional context in comments.

4 Comments

I tried that but that didn't help. The error still appears. Weirdly enough when I check the database, the foreign keys do appear so I don't understand the error. Any other idea of what could be causing it?
I had the same issue because I used and generated migrations on a sqlite database in the dev env but my production database was powered by mysql.
This fixed it for me except my migrations are in src/Migrations
My answer was written for symfony 3. In symfony 4 and above, you are right, the migrations are in src/Migrations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.