Doctrine迁移和fixtures错误

时间:2016-05-10 13:42:55

标签: symfony doctrine-orm

我执行迁移来创建数据库,之后,我想用一些数据填充数据库。为什么生成id策略不起作用?

bin/console doctrine:migrations:migrate   

                    Migrations                    


WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)y
Migrating up to 20160510103145 from 0

  ++ migrating 20160510103145

     -> CREATE TABLE navieras (id INTEGER NOT NULL, nombre VARCHAR(255) NOT NULL)

  ++ migrated (0.14s)

  ------------------------

  ++ finished in 0.14s
  ++ 1 migrations executed
  ++ 1 sql queries




bin/console doctrine:fixtures:load     
Careful, database will be purged. Do you want to continue y/N ?y
  > purging database
  > loading AppBundle\DataFixtures\ORM\LoadNavieraData


  [Doctrine\DBAL\Exception\NotNullConstraintViolationException]                                                      
  An exception occurred while executing 'INSERT INTO navieras (nombre) VALUES (?)' with params ["Royal Caribbean"]:  
  SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: navieras.id                        



  [Doctrine\DBAL\Driver\PDOException]                                                          
  SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: navieras.id  



  [PDOException]                                                                               
  SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: navieras.id  

夹具加载功能

public function load(ObjectManager $manager)
{
    $naviera = new Naviera();
    $naviera->setNombre('aaaaaaaaaaaaaaa');

    $naviera2 = new Naviera();
    $naviera2->setNombre('bbbbbbbbbbbbbb');

    $manager->persist($naviera);
    $manager->persist($naviera2);
    $manager->flush();
}

移植

public function up(Schema $schema)
{
    $navieras = $schema->createTable('navieras');
    $navieras->addColumn('id', "integer");
    $navieras->addColumn('nombre', "string");
}

/**
 * @param Schema $schema
 */
public function down(Schema $schema)
{
    $schema->dropTable('navieras');
}  

Naviera实体

/**
 * Modelo naviera
 *
 * @Entity
 * @Table(name="navieras")
 */
class Naviera
{
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue
     */
    private $id;

    /**
     * @Column(type="string");
     */
    private $nombre;

    /**
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param integer $id
     */
    public function setId($id)
    {
        $this->$id = $id;
    }

    /**
     * @return string
     */
    public function getNombre()
    {
        return $this->nombre;
    }

    /**
     * @param string $nombre
     */
    public function setNombre($nombre)
    {
        $this->nombre = $nombre;
    }

}

我正在使用SQLite

1 个答案:

答案 0 :(得分:1)

您的向上迁移不会创建自动递增ID列。只是一个整数列。如果您将迁移功能更改为以下内容,则应解决您的问题:

public function up(Schema $schema)
{
    $navieras = $schema->createTable('navieras');
    $navieras->addColumn('id', 'integer', ['Autoincrement' => true]);
    $navieras->addColumn('nombre', 'string');
    $navieras->setPrimaryKey('id', true);
}