10

次の移行を適用しようとしています。

Schema::table('users', function (Blueprint $table) {
    $table->timestamp('created_at')->useCurrent()->change();
});

しかし、次のartisanように述べています。

  [Doctrine\DBAL\DBALException]
  Unknown column type "timestamp" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL
  \Types\Type::addType(). You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). I
  f this error occurs during database introspection then you might have forgot to register all database types for a
  Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMapp
  edDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping inform
  ation.

mmerian/doctrine-timestamp( composer install mmerian/doctrine-timestamp)をインストールしようとすると、次のように表示されcomposerます。

  [InvalidArgumentException]
  Could not find package mmerian/doctrine-timestamp at any version for your minimum-stability (stable). Check the pa
  ckage spelling or your minimum-stability

私は何をしますか?

UPD Withcomposer require mmerian/doctrine-timestamp=dev-masterで、パッケージをインストールしてからステートメントのType::addType('timestamp', 'DoctrineTimestamp\DBAL\Types\Timestamp');前に追加できましたが、他のエラーが発生しました:Schema::table

  [Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'created_at' (SQL: ALTER TABLE u
  sers CHANGE created_at created_at INT DEFAULT 'CURRENT_TIMESTAMP' NOT NULL)

UPDmmerian/doctrine-timestamp当時のドキュメントの最初の行のみを追加したため(またはドキュメントが更新されたため)、で動作するかどうかを再度確認しました。

Type::addType('timestamp', 'DoctrineTimestamp\DBAL\Types\Timestamp');                                          
DB::getDoctrineConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('Timestamp', 'timestamp');

しかし、それも役に立ちません。移行は成功しますが、列の定義は変更されません。

4

6 に答える 6

6

ご覧のとおりmmerian/doctrine-timestamp、問題は解決しません。まず、この行 $table->getColumns()['created_at']の後に

class Doctrine\DBAL\Schema\Column#520 (16) {
  protected $_type => class Doctrine\DBAL\Types\DateTimeType#504 (0) { }
  protected $_length => NULL
  protected $_precision => int(10)
  protected $_scale => int(0)
  protected $_unsigned => bool(false)
  protected $_fixed => bool(false)
  protected $_notnull => bool(true)
  protected $_default => string(17) "CURRENT_TIMESTAMP"
  protected $_autoincrement => bool(false)
  protected $_platformOptions => array(0) { }
  protected $_columnDefinition => NULL
  protected $_comment => NULL
  protected $_customSchemaOptions => array(0) { }
  protected $_name => string(10) "created_at"
  protected $_namespace => NULL
  protected $_quoted => bool(false)
}

そして$this->getTableWithColumnChanges($blueprint, $table)->getColumns()['created_at']_

class Doctrine\DBAL\Schema\Column#533 (16) {
  protected $_type => class DoctrineTimestamp\DBAL\Types\Timestamp#513 (0) { }
  protected $_length => NULL
  protected $_precision => int(10)
  protected $_scale => int(0)
  protected $_unsigned => bool(false)
  protected $_fixed => bool(false)
  protected $_notnull => bool(true)
  protected $_default => string(17) "CURRENT_TIMESTAMP"
  protected $_autoincrement => bool(false)
  protected $_platformOptions => array(0) { }
  protected $_columnDefinition => NULL
  protected $_comment => NULL
  protected $_customSchemaOptions => array(0) { }
  protected $_name => string(10) "created_at"
  protected $_namespace => NULL
  protected $_quoted => bool(false)
}

というわけで、まずこちらのパーツの情報が見当たりませんON UPDATE。第二に、唯一の違いは$_type価値です。この行の後に確認できるの$tableDiff->changedColumns['created_at']->changedPropertiesは、

array(1) {
  [0] => string(4) "type"
}

次に、ステートメントを生成 すると、すべてこれになりますALTER TABLE

public function getDefaultValueDeclarationSQL($field)
{
    $default = empty($field['notnull']) ? ' DEFAULT NULL' : '';
    if (isset($field['default'])) {
        $default = " DEFAULT '".$field['default']."'";
        if (isset($field['type'])) {
            if (in_array((string) $field['type'], array("Integer", "BigInt", "SmallInt"))) {
                $default = " DEFAULT ".$field['default'];
            } elseif (in_array((string) $field['type'], array('DateTime', 'DateTimeTz')) && $field['default'] == $this->getCurrentTimestampSQL()) {
                $default = " DEFAULT ".$this->getCurrentTimestampSQL();
            } elseif ((string) $field['type'] == 'Time' && $field['default'] == $this->getCurrentTimeSQL()) {
                $default = " DEFAULT ".$this->getCurrentTimeSQL();
            } elseif ((string) $field['type'] == 'Date' && $field['default'] == $this->getCurrentDateSQL()) {
                $default = " DEFAULT ".$this->getCurrentDateSQL();
            } elseif ((string) $field['type'] == 'Boolean') {
                $default = " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
            }
        }
    }
    return $default;
}

この行のどこかに、 Timestamptype が に変わるチェック'CURRENT_TIMESTAMP'があるはずCURRENT_TIMESTAMPです。これは可能mmerian/doctrine-timestampですか?その質問は今のところ未解決のままです。このチェックは、おそらく私の特定の問題を解決するでしょう。しかし、今のところ、私はこれで逃げるつもりです:

DB::statement('ALTER TABLE users MODIFY COLUMN created_at
    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP');
于 2016-01-14T06:45:33.953 に答える
0

Doctrine はこれをサポートしたくないので、MySQL 固有の列タイプであるため、私はこれを作成しました。

于 2016-11-24T16:24:33.100 に答える