Doctrine generate-migrations-diff和migrate

时间:2010-06-17 08:23:04

标签: php orm doctrine command-line-interface migration

我尝试执行cli命令./doctrine generate-migrations-diff,并在正确的文件夹中正确创建了一个版本文件。

消息是: generate-migrations-diff - 从差异中成功生成的迁移类

然后我尝试执行另一个cli命令./doctrine migrate并向我显示一条消息: migrate - 成功迁移到版本#1 ,但是当我打开该类时,任何修改都已完成。为什么呢?

这是version1文件:

<?php

class Version1 extends Doctrine_Migration_Base
{
public function up()
{
    $this->removeColumn('addresses', 'address_test');
}

public function down()
{
    $this->addColumn('addresses', 'address_test', 'string', '', array(
         'fixed' => '0',
         'unsigned' => '',
         'primary' => '',
         'notnull' => '1',
         'autoincrement' => '',
         ));
}
}

?>

这是YAML。 我删除了字段:address_test

Addresses:
  connection: doctrine
  tableName: addresses
  columns:
    address_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    address:
      type: string()
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    city:
      type: string(150)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    code:
      type: string(20)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    country_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    base:
      type: integer(1)
      fixed: false
      unsigned: false
      primary: false
      default: '0'
      notnull: true
      autoincrement: false
    latitude:
      type: string(20)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    longitude:
      type: string(20)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    customer_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false

这是迁移命令生成的BaseAddresses类:     bindComponent('Addresses','doctrine');

/**
 * BaseAddresses
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 * 
 * @property integer $address_id
 * @property string $address
 * @property string $city
 * @property string $code
 * @property integer $country_id
 * @property integer $base
 * @property string $latitude
 * @property string $longitude
 * @property integer $customer_id
 * @property Countries $Countries
 * @property Customers $Customers
 * 
 * @package    ##PACKAGE##
 * @subpackage ##SUBPACKAGE##
 * @author     ##NAME## <##EMAIL##>
 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
 */
abstract class BaseAddresses extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('addresses');
        $this->hasColumn('address_id', 'integer', 4, array(
             'type' => 'integer',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => true,
             'autoincrement' => true,
             'length' => '4',
             ));
        $this->hasColumn('address', 'string', null, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             'length' => '',
             ));
        $this->hasColumn('city', 'string', 150, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             'length' => '150',
             ));
        $this->hasColumn('code', 'string', 20, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             'length' => '20',
             ));
        $this->hasColumn('country_id', 'integer', 4, array(
             'type' => 'integer',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             'length' => '4',
             ));
        $this->hasColumn('base', 'integer', 1, array(
             'type' => 'integer',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'default' => '0',
             'notnull' => true,
             'autoincrement' => false,
             'length' => '1',
             ));
        $this->hasColumn('latitude', 'string', 20, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             'length' => '20',
             ));
        $this->hasColumn('longitude', 'string', 20, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             'length' => '20',
             ));
        $this->hasColumn('customer_id', 'integer', 4, array(
             'type' => 'integer',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             'length' => '4',
             ));
    }

    public function setUp()
    {
        parent::setUp();
        $this->hasOne('Countries', array(
             'local' => 'country_id',
             'foreign' => 'country_id'));

        $this->hasOne('Customers', array(
             'local' => 'customer_id',
             'foreign' => 'customer_id',
             'onDelete' => 'CASCADE'));
    }
}

在某些网站上我已经读过我必须执行命令: build-all 以生成更新类地址的版本,但是我收到此错误:

SQLSTATE[HY000]: General error: 1005 Can't create table 'web63db1.#sql-3e6_11d' (errno: 121). Failing Query: "ALTER TABLE addresses ADD CONSTRAINT addresses_customer_id_customers_customer_id FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE CASCADE". Failing Query: ALTER TABLE addresses ADD CONSTRAINT addresses_customer_id_customers_customer_id FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE CASCADE

我做了什么?提前谢谢。

2 个答案:

答案 0 :(得分:7)

迁移仅用于更改数据库,而不用于更改模型类。您需要在之后构建模型类。

所以你的正常工作方式是:

  1. 更改schema.yml
  2. 创建迁移文件(generate-migrations-diff)
  3. 针对数据库运行迁移(迁移),现在应该更改数据库
  4. 更新您的模型类(build-all)

答案 1 :(得分:1)

由于我无法评论,让我修改Timo的答案:

  1. 更改您的schema.yml
  2. 创建迁移文件(doctrine:generate-migrations-diff)(注意:这是将模式与类文件进行比较
  3. 运行迁移(doctrine:migrate)(这会根据数据库中的迁移版本号运行迁移)
  4. 构建您的类文件。 (doctrine:build --all-classes OR doctrine:build-model&amp; doctrine:build-forms&amp; doctrine:build-filters)(注意:这将构建所有类文件,但不会尝试更新您的数据库。)
  5. 我强烈建议您在首次运行任何迁移之前备份数据库。如果迁移失败(即因为您的架构错误),您最终可能会遇到一个在迁移之间陷入困境的数据库,需要报废。我通常,备份我的test-db然后导入实时数据库以测试迁移。对于在实时服务器上迁移失败,我有点害怕。

    重要的是要注意'doctrine:generate-migrations-diff'将schema.yml与当前生成的类文件进行比较。 在生成迁移之前不要构建类

    要在服务器上部署,您需要上传schema.yml和新的迁移文件。然后做第3步和第3步4(然后根据习惯清除你的缓存),你很高兴。