Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with yii\db\ColumnSchemaBuilder #18844

Closed
WinterSilence opened this issue Aug 18, 2021 · 2 comments
Closed

Problems with yii\db\ColumnSchemaBuilder #18844

WinterSilence opened this issue Aug 18, 2021 · 2 comments
Labels
Milestone

Comments

@WinterSilence
Copy link
Contributor

WinterSilence commented Aug 18, 2021

yii\db\ColumnSchemaBuilder not support:

  • AUTO_INCREMENT for primary keys
  • column sub-type(integer, string) for types Schema::TYPE_*PK

key is index, not column!
To keep BC and fix that's problems vote to add:

/**
 * @var string the auto increment representation, must be overriden in current DB extension (ex.: MySql - `AUTO_INCREMENT`, PostgreSql - `SERIAL`)
 */
const AUTO_INCREMENT = '';
/**
 * @var string the column type of key
 */
public $keyType;
/**
 * @var bool the flag `AUTO_INCREMENT` to primary key (`Schema::TYPE_*PK`)
 */
public $isAutoIncrement = false;

public function __construct($type, $length = null, $db = null, $config = [])
{
    $this->type = $type;
    $this->length = $length;
    $this->db = $db;
     if ($this->getTypeCategory() === self::CATEGORY_PK) {
          $this->isAutoIncrement = true;
     }

    parent::__construct($config);
}

public function autoIncrement()
{
     if ($this->getTypeCategory() === self::CATEGORY_PK && $this->keyType !== self::CATEGORY_STRING) {
          $this->isAutoIncrement = true;
     }

     return $this;
}

protected function buildAutoIncrementString()
{
    return $this->isAutoIncrement ? ' ' . static::AUTO_INCREMENT : '';
}

public function keyType($type)
{
     if ($this->getTypeCategory() === self::CATEGORY_PK) {
          if (!in_array($type, self::$typeCategoryMap, true) || !in_array(static::$typeCategoryMap[$type], [self::CATEGORY_STRING, self::CATEGORY_NUMERIC], true)) {
               throw new InvalidArgumentException('Invalid column type: ' . $type);
          }
          $this->keyType = $type;
          if ($this->getTypeCategory() === self::CATEGORY_STRING) {
               $this->isAutoIncrement = false;
          }
     }

     return $this;
}

protected function buildCompleteString($format)
{
    $placeholderValues = [
        '{type}' => $this->type,
        '{length}' => $this->buildLengthString(),
        '{unsigned}' => $this->buildUnsignedString(),
        '{notnull}' => $this->buildNotNullString(),
        '{unique}' => $this->buildUniqueString(),
        '{default}' => $this->buildDefaultString(),
        '{check}' => $this->buildCheckString(),
        '{length}' => $this->buildAutoIncrementString(),
        '{autoincrement}' => $this->buildCommentString(),
        '{pos}' => $this->isFirst ? $this->buildFirstString() : $this->buildAfterString(),
        '{append}' => $this->buildAppendString(),
    ];

    return strtr($format, $placeholderValues);
}

public function __toString()
{
    switch ($this->getTypeCategory()) {
        case self::CATEGORY_PK:
            $format = '{type}{check}{autoincrement}{comment}{append}';
            break;
        default:
            $format = '{type}{length}{notnull}{unique}{default}{check}{comment}{append}';
    }

    return $this->buildCompleteString($format);
}

Additional bug in migration:
> yii migrate/create app\migrations\CreateDemoTable --fields="id:integer(11):unsigned:notNull,name:string(50)
generate array with 2 'id' keys.

Additional info

Q A
Yii version 2.0.44
PHP version any
Operating system any
@yii-bot
Copy link

yii-bot commented Aug 18, 2021

Thanks for posting in our issue tracker.
In order to properly assist you, we need additional information:

  • When does the issue occur?
  • What do you see?
  • What was the expected result?
  • Can you supply us with a stacktrace? (optional)
  • Do you have exact code to reproduce it? Maybe a PHPUnit tests that fails? (optional)

Thanks!

This is an automated comment, triggered by adding the label status:need more info.

@samdark
Copy link
Member

samdark commented Aug 18, 2021

Not a bug. PK is a special shortcut-type and it's documented in the guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants