I am using doctrine with symfony2 and sqlite database and I have a problem inserting values as the id is not incremented anymore. I just changed a field annotation and I have drop the database and created it again with:
php app/console doctrine:database:create --env=dev
php app/console doctrine:schema:update --force
response:
Database schema updated successfully! "4" queries were executed
I have done the update a second time and I had
Database schema updated successfully! "14" queries were executed
I have a class "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();
}
Now I have done this to insert:
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();
}
I get this error the second time I execute this function:
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
So we can see here that the autoincrement didn't work(the id should be '2').
I also cleared the symfony cache but it didn't change anything.
However if I change the $author type to integer(instead of join), the autoincrement works.
Thanks in advance for any help.
Thanks to Artamiel, the problem was with column names. "Since you did not set a name for $id Doctrine automatically converts the name of the property to a column name. And then when we look at your $author property we clearly see this - JoinColumn(name="id") which causes the problem. Change the name to author_id for instance and you will be good to go"