0

I'm having trouble with foreign keys in doctrine. I have a "User"-table and a "Orders"-table. Now I want to have a foreign key from userID in the orders-table to the id in the user-table. The orders-entity looks like:

<?php namespace Application\TestBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table( * name="Orders", * options={"collate"="utf8_general_ci", * "charset"="utf8", * "engine"="InnoDB" * } * ) * @ORM\Entity(repositoryClass="Application\TestBundle\Entity\OrdersRepository") */ class Orders { /** * @var integer * * @ORM\Column( * name="id", * unique=true, * type="integer", * options={"unsigned"=true} * ) * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var integer * * @ORM\Column( * name="userID", * type="integer", * nullable=false, * options={"unsigned"=true} * ) * @ORM\OneToOne(targetEntity="User") * @ORM\JoinColumn(name="userID", referencedColumnName="id") */ private $userID; // ... } 

The user-entity looks like:

<?php namespace Application\TestBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table( * name="User", * options={"collate"="utf8_general_ci", * "charset"="utf8", * "engine"="InnoDB" * } * ) * @ORM\Entity(repositoryClass="Application\TestBundle\Entity\UserRepository") */ class User { /** * @var integer * * @ORM\Column( * name="id", * unique=true, * type="integer", * options={"unsigned"=true} * ) * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; // ... } 

Unfortunately no foreign key will be set. It is not in the SQL-query. What am I doing wrong?

1
  • How do you persist your entity? Can you paste the relevant part from your controller action? Commented Jun 16, 2014 at 10:05

2 Answers 2

4

The solution is simple. It can only be a relation or a datatype (string, object, int). So my example requires a relation, so I had to remove the @ORM\Column-annotation and it worked!

Sign up to request clarification or add additional context in comments.

Comments

0

This usually happens if your database table engine type does not support foreign keys (such as MyISAM). Try changing your engine type for example to InnoDB, then run

console doctrine:schema:update 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.