我执行迁移来创建数据库,之后,我想用一些数据填充数据库。为什么生成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 AppBundleDataFixturesORMLoadNavieraData
[DoctrineDBALExceptionNotNullConstraintViolationException]
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
[DoctrineDBALDriverPDOException]
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
向上迁移不会创建自动递增的id列。只是一个整数列。如果您将迁移功能更改为以下功能,它应该会解决您的问题:
public function up(Schema $schema)
{
$navieras = $schema->createTable('navieras');
$navieras->addColumn('id', 'integer', ['Autoincrement' => true]);
$navieras->addColumn('nombre', 'string');
$navieras->setPrimaryKey('id', true);
}