Всем привет!
Подскажите как в Yii2 при создании миграции для первичного ключа новой таблицы указать тип данных UUID, СУБД PostgreSQL.
Заранее благодарю!
UUID как первичный ключ для Postgre
-
- Сообщения: 910
- Зарегистрирован: 2019.08.13, 01:49
Re: UUID как первичный ключ для Postgre
Делаете трейт
Подключаете его в классе вашей миграции, используете как обычно.
Не проверял, но должно работать.
Код: Выделить всё
trait UuidTypeTrait
{
/**
* @return Connection the database connection to be used for schema building.
*/
abstract protected function getDb();
/**
* Creates a uuid column.
* @return ColumnSchemaBuilder the column instance which can be further customized.
*/
public function uuid()
{
return $this->getDb()->getSchema()->createColumnSchemaBuilder('uuid');
}
}
Код: Выделить всё
$this->createTable('{{%user}}', [
'id' => $this->uuid()->primaryKey(),
'username' => $this->string()->notNull()->unique(),
]);
-
- Сообщения: 910
- Зарегистрирован: 2019.08.13, 01:49
Re: UUID как первичный ключ для Postgre
Хотя не, соврал немного. Вот так будет работать.
Код: Выделить всё
$this->createTable('{{%user}}', [
'id' => $this->uuid(),
'username' => $this->string()->notNull()->unique(),
]);
$this->addPrimaryKey('pk-user', '{{%user}}', 'id');