【问题标题】:MySQL error when migrating database schema, Doctrine with Symfony迁移数据库模式时出现 MySQL 错误,使用 Symfony 的 Doctrine
【发布时间】:2017-03-26 08:02:41
【问题描述】:

我正在使用 Symfony 框架和 MySQL 作为数据库做一个 Web 应用程序。我正在使用学说来做 ORM 来映射数据库。我创建了架构并执行了学说:迁移:差异来验证查询并检查我的关系是否按顺序排列以及它们看起来是否正确。但是当我想将新架构迁移到 MySQL 时,会出现以下错误:Picture of the Command Line describing the error

这是商务舱

/**
* @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;
}
}

这是客户端类:

<?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;
}   
}

这是迁移:

命名空间应用\迁移;

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');
}
}

架构包含更多类,客户端和业务实体包含更多属性,但为了简单起见,我只发布了产生错误的那些。我会感谢任何人的帮助,因为这是我第一次使用这些技术,我有点迷失了。

【问题讨论】:

    标签: php mysql symfony doctrine-orm


    【解决方案1】:

    在开发阶段,我经常需要“清除”以前的迁移,然后重新开始。如果您的数据库正在生产中,您不应该这样做!要重新开始,您可以删除数据库,重新创建它,删除以前的迁移,然后重新运行 diff 并迁移。在 symfony3 中,我这样做:

    // 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
    

    【讨论】:

    • 我试过了,但没有帮助。错误仍然出现。奇怪的是,当我检查数据库时,确实出现了外键,所以我不明白这个错误。关于可能导致它的任何其他想法?
    • 我遇到了同样的问题,因为我在 dev env 中的 sqlite 数据库上使用并生成了迁移,但我的生产数据库由 mysql 提供支持。
    • 这为我修复了它,除了我的迁移在 src/Migrations 中
    • 我的答案是为 symfony 3 写的。在 symfony 4 及更高版本中,你是对的,迁移在 src/Migrations
    猜你喜欢
    • 2016-06-14
    • 1970-01-01
    • 2020-01-15
    • 2019-02-14
    • 2020-11-28
    • 2022-01-11
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多