我使用的学说与symfony2和sqlite数据库,我有插入值的问题,因为id不再增加了。我只是改变了一个字段注释,我已经删除了数据库,并重新创建了它:
php app/console doctrine:database:create --env=dev
php app/console doctrine:schema:update --force
反应:
Database schema updated successfully! "4" queries were executed
我已经做了第二次更新,我有
Database schema updated successfully! "14" queries were executed
我有一个类Post:
namespace Llafon'BlogBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
* @ORM'Table(name="post")
* @ORM'Entity
*/
class Post {
/**
* @ORM'Column(type="integer")
* @ORM'Id
* @ORM'GeneratedValue(strategy="IDENTITY") //I also tried "AUTO"
*/
protected $id;
/**
* @ORM'Column(type="string", length=100, nullable=false)
*/
protected $title;
/**
* @ORM'Column(type="text", nullable=false)
*/
protected $content;
/**
* @ORM'Column(type="datetime", nullable=false)
*/
protected $date;
/**
* @ORM'ManyToOne(targetEntity="User") //This variable was an integer before
* @ORM'JoinColumn(name="id", referencedColumnName="id")
*/
protected $author;
/**
* @ORM'Column(type="string", nullable=false)
*/
protected $image_name;
public function __construct() {
$this->date = new 'DateTime();
}
插入:
public function addAction(Request $request) {
$post = new Post();
$user = $this->container->get('security.context')->getToken()->getUser();
$post->setAuthor($user); //$user is not null
$post->setContent("zz");
$post->setImageName("aaa");
$post->setTitle("fff");
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();die();
}
当我第二次执行这个函数时,我得到了这个错误:
An exception occurred while executing 'INSERT INTO post (title, content, date, image_name, id) VALUES (?, ?, ?, ?, ?)' with params ["fff", "zz", "2015-10-25 21:45:37", "aaa", 1]:
SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: post.id
所以我们可以看到这里的自动递增没有工作(id应该是'2')。
我还清除了symfony缓存,但它没有改变任何东西。
但是,如果我将$author类型更改为整数(而不是join),则自动增量工作。
提前感谢您的帮助。
多亏了Artamiel,问题出在列名上。"由于没有为$id设置名称,Doctrine会自动将属性的名称转换为列名。然后,当我们看你的$author属性,我们清楚地看到这个- JoinColumn(name="id"),这导致了问题。例如,将名称更改为author_id然后就可以使用"