尝试进行迁移

问题描述 投票:0回答:1

这是我的错误

[notice] Migrating up to DoctrineMigrations\Version20231003115032
[error] Migration DoctrineMigrations\Version20231003115032 failed during Execution. Error: "An exception occurred while executing a query: SQLSTATE[22004]: Null value not allowed: 1138 Invalid use of NULL value"

In ExceptionConverter.php line 114:
                                                                                                                          
  An exception occurred while executing a query: SQLSTATE[22004]: Null value not allowed: 1138 Invalid use of NULL value  
                                                                                                                          

In Exception.php line 28:
                                                                           
  SQLSTATE[22004]: Null value not allowed: 1138 Invalid use of NULL value  
                                                                           

In Connection.php line 70:
                                                                           
  SQLSTATE[22004]: Null value not allowed: 1138 Invalid use of NULL value

我刚刚添加

cascade: ["persist"]

在我的订单实体中

#[ORM\OneToMany(mappedBy: 'orders', targetEntity: OrdersDetails::class, cascade: ["persist"], orphanRemoval: true)]
private $ordersDetails;

我必须遵循一条原则:迁移:迁移,否则我的网站将无法工作。

<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20231003115032 extends AbstractMigration
{
    public function getDescription(): string
    {
        return '';
    }

    public function up(Schema $schema): void
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->addSql('ALTER TABLE categories DROP FOREIGN KEY FK_3AF34668727ACA70');
        $this->addSql('ALTER TABLE categories ADD CONSTRAINT FK_3AF34668727ACA70 FOREIGN KEY (parent_id) REFERENCES categories (id)');
        $this->addSql('ALTER TABLE users CHANGE reset_token reset_token VARCHAR(100) NOT NULL');
    }

    public function down(Schema $schema): void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->addSql('ALTER TABLE categories DROP FOREIGN KEY FK_3AF34668727ACA70');
        $this->addSql('ALTER TABLE categories ADD CONSTRAINT FK_3AF34668727ACA70 FOREIGN KEY (parent_id) REFERENCES categories (id) ON UPDATE NO ACTION ON DELETE CASCADE');
        $this->addSql('ALTER TABLE users CHANGE reset_token reset_token VARCHAR(100) DEFAULT NULL');
    }
}

我在我的用户实体中为reset_token表添加了一个默认空值,并且它已经像我的数据库中那样定义了。我认为是因为这个,但我不确定。

php sql mysql symfony migration
1个回答
0
投票

这是一系列事件:

  1. 您有一个可选字段。

  2. 您可以在该字段为空的地方添加记录。

  3. 您将该字段设置为必填字段。

    ALTER TABLE users CHANGE reset_token reset_token VARCHAR(100) NOT NULL
    

您不能有空白的必填字段,因此会出现错误。

您有几种选择:

  • 在字段级别设置默认值。

    @ORM\Column(name="reset_token", type="string", length=100, nullable=false, options={"default" : "blah blah blah"})
    
  • 为现有记录找出适当的值并在更改表之前更新它:

    $this->addSql('UPDATE users SET reset_token = (/* maybe some complex expression*/);
    
  • 删除现有记录:

    $this->addSql('DELETE FROM users WHERE reset_token IS NULL');
    

一般而言,方法没有好坏之分,这完全取决于您特定的业务规则。从表名和列名来看,我怀疑您首先不希望该字段是强制性的,因为除非用户想要重置密码,否则不希望用户拥有重置令牌。但我只是猜测,我不知道你用这个柱子做什么。

© www.soinside.com 2019 - 2024. All rights reserved.