This is my error
[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
I just added
cascade: ["persist"]
In my orders Entity
#[ORM\OneToMany(mappedBy: 'orders', targetEntity: OrdersDetails::class, cascade: ["persist"], orphanRemoval: true)]
private $ordersDetails;
And i have to do a doctrine:migration:migrate otherwise my site will not work.
<?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');
}
}
I added a default null in my users entity for reset_token table to, and it's already defined like that in my db. I think it's because of this but i'm not sure .
This is the series of events:
You have an optional field.
You add records where that field is blank.
You make the field mandatory.
ALTER TABLE users CHANGE reset_token reset_token VARCHAR(100) NOT NULL
You can't have a blank mandatory field, thus the error.
You have a few alternatives here:
Set a default value at field level.
@ORM\Column(name="reset_token", type="string", length=100, nullable=false, options={"default" : "blah blah blah"})
Figure out an appropriate value for existing records and update it before altering the table:
$this->addSql('UPDATE users SET reset_token = (/* maybe some complex expression*/);
Delete existing records:
$this->addSql('DELETE FROM users WHERE reset_token IS NULL');
In general terms, there's no good or bad approach, it all depends on your particular business rules. Judging by the table and column names, I suspect that you don't want the field to be mandatory in the first place, since users aren't expected to have a reset token unless they want to reset their password. But I'm just speculating, I don't know what you use the column for.