代码之家  ›  专栏  ›  技术社区  ›  Nemus

将新字段添加到实体类后违反完整性约束

  •  0
  • Nemus  · 技术社区  · 6 年前

       /**
        * @ORM\Column(type="string", length=255)
        */
        private $file;
    

    我跑了: php bin/console doctrine:migrations:diff

    这是它生成的迁移文件:

    <?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 Version20181019082359 extends AbstractMigration
    {
        public function up(Schema $schema) : void
        {
            // this up() migration is auto-generated, please modify it to your needs
            $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'sqlite', 'Migration can only be executed safely on \'sqlite\'.');
    
            $this->addSql('DROP INDEX IDX_D592642C12469DE2');
            $this->addSql('CREATE TEMPORARY TABLE __temp__wallpaper AS SELECT id, category_id, filename, slug, width, height FROM wallpaper');
            $this->addSql('DROP TABLE wallpaper');
            $this->addSql('CREATE TABLE wallpaper (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, category_id INTEGER DEFAULT NULL, filename VARCHAR(255) NOT NULL COLLATE BINARY, slug VARCHAR(255) DEFAULT NULL COLLATE BINARY, width INTEGER NOT NULL, height INTEGER NOT NULL, file VARCHAR(255) NOT NULL, CONSTRAINT FK_D592642C12469DE2 FOREIGN KEY (category_id) REFERENCES category (id) NOT DEFERRABLE INITIALLY IMMEDIATE)');
            $this->addSql('INSERT INTO wallpaper (id, category_id, filename, slug, width, height) SELECT id, category_id, filename, slug, width, height FROM __temp__wallpaper');
            $this->addSql('DROP TABLE __temp__wallpaper');
            $this->addSql('CREATE INDEX IDX_D592642C12469DE2 ON wallpaper (category_id)');
        }
    
        public function down(Schema $schema) : void
        {
            // this down() migration is auto-generated, please modify it to your needs
            $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'sqlite', 'Migration can only be executed safely on \'sqlite\'.');
    
            $this->addSql('DROP INDEX IDX_D592642C12469DE2');
            $this->addSql('CREATE TEMPORARY TABLE __temp__wallpaper AS SELECT id, category_id, filename, slug, width, height FROM wallpaper');
            $this->addSql('DROP TABLE wallpaper');
            $this->addSql('CREATE TABLE wallpaper (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, category_id INTEGER DEFAULT NULL, filename VARCHAR(255) NOT NULL, slug VARCHAR(255) DEFAULT NULL, width INTEGER NOT NULL, height INTEGER NOT NULL)');
            $this->addSql('INSERT INTO wallpaper (id, category_id, filename, slug, width, height) SELECT id, category_id, filename, slug, width, height FROM __temp__wallpaper');
            $this->addSql('DROP TABLE __temp__wallpaper');
            $this->addSql('CREATE INDEX IDX_D592642C12469DE2 ON wallpaper (category_id)');
        }
    }
    

    之后: php bin/console doctrine:migrations:migrate

    我得到一个错误:

    Migration 20181019082359 failed during Execution. Error An exception occurred while executing 'INSERT INTO wallpaper (id, category_id, filename, slug, width, height) SELECT id, category_id, filename, slug, width, height FROM __temp__wallpaper':
    
    SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: wallpaper.file
    
    In AbstractSQLiteDriver.php line 59:
    
      An exception occurred while executing 'INSERT INTO wallpaper (id, category_id, filename, slug, width, height) SELECT id, category_id, filename, slug, w  
      idth, height FROM __temp__wallpaper':                                                                                                                    
    
      SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: wallpaper.file                                                           
    
    
    In PDOConnection.php line 109:
    
      SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: wallpaper.file  
    
    
    In PDOConnection.php line 107:
    
      SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: wallpaper.file  
    

    0 回复  |  直到 6 年前