Symfony2 ORM随机添加约束外键

时间:2015-01-26 12:25:28

标签: symfony orm doctrine-orm foreign-keys entity

我想创建一个表来列出我的书。我定义字段但由于某种原因它创建外键,我不明白。为什么会这样?

控制台日志:

C:\xampp\htdocs\test2>php app\console doctrine:schema:update --dump-sql
ALTER TABLE 2_00_book CHANGE kword kword integer unsigned, CHANGE page page integer unsigned;
ALTER TABLE 2_00_book ADD CONSTRAINT FK_96339B585E9E89CB FOREIGN KEY (location)REFERENCES 2_08_location (id);
ALTER TABLE 2_00_book ADD CONSTRAINT FK_96339B589CE8D546 FOREIGN KEY (publisher) REFERENCES 2_07_publisher (id);
ALTER TABLE 2_01_book ADD CONSTRAINT FK_5D6F48FD36BB5955 FOREIGN KEY (bookid) REFERENCES 2_00_book (id);
ALTER TABLE 2_02_review ADD CONSTRAINT FK_A75BB31DD4E70683 FOREIGN KEY (headlineid) REFERENCES 2_01_book (id);
ALTER TABLE 2_04_taglist ADD CONSTRAINT FK_6AB8110436BB5955 FOREIGN KEY (bookid) REFERENCES 2_00_book (id);

C:\xampp\htdocs\test2>php app\console doctrine:schema:update --force
Updating database schema...



  [Doctrine\DBAL\Exception\DriverException]
  An exception occurred while executing 'ALTER TABLE 2_00_book ADD CONSTRAINT FK_96339B585E9E89CB FOREIGN KEY (location) REFERENCES 2_08_location (id)':
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

这是导致错误的实体:

class Book
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="title", type="string", length=200, options={"fixed":false}, nullable=false)
 */
private $title;

/**
 * @var string
 *
 * @ORM\Column(name="author", type="string", length=200, options={"fixed":false}, nullable=false)
 */
private $author;

/**
 * @var string
 *
 * @ORM\Column(name="region", type="string", length=40, options={"fixed":false}, nullable=false)
 */
private $region;

/**
 * @var string
 *
 * @ORM\Column(name="copyrighter", type="string", length=100, options={"fixed":false}, nullable=false)
 */
private $copyrighter;

/**
 * @var boolean
 *
 * @ORM\Column(name="translated", type="boolean", nullable=false)
 */
private $translated;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="purchdate", type="date", nullable=false)
 */
private $purchdate;

/**
 * @var float
 *
 * @ORM\Column(name="price", type="float", nullable=false)
 */
private $price;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="pubdate", type="date", nullable=false)
 */
private $pubdate;

/**
 * @var \DateTime
 *
 * @ORM\Column(nullable=false)
 */
private $printdate;

/**
 * @var string
 *
 * @ORM\Column(type="string", length=5, options={"fixed":false}, nullable=false)
 */
private $ver;

/**
 * @var string
 *
 * @ORM\Column(type="string", length=6, options={"fixed":false}, nullable=false)
 */
private $deco;

/**
 * @var integer
 *
 * @ORM\Column(columnDefinition="integer unsigned", nullable=false)
 */
private $kword;

/**
 * @var integer
 *
 * @ORM\Column(columnDefinition="integer unsigned", nullable=false)
 */
private $page;

/**
 * @var string
 *
 * @ORM\Column(type="string", length=17, options={"fixed":false}, nullable=false)
 * @ORM\OneToMany(targetEntity="Taglist", mappedBy="bookid")
 * @ORM\OneToMany(targetEntity="Headline", mappedBy="bookid")
 */
private $isbn;

/**
 * @var string
 *
 * @ORM\Column(type="string", length=12, options={"fixed":false}, nullable=false)
 */
private $category;

/**
 * @var string
 *
 * @ORM\Column(type="string", length=2, options={"fixed":false}, nullable=false)
 */
private $location;

/**
 * @var string
 *
 *@ORM\Column(name="intro", type="text", nullable=false)
 */
private $intro;

/**
 * @var boolean
 *
 * @ORM\Column(name="instock", type="boolean", nullable=false)
 */
private $instock;

/**
 * @var \Books\BooksBundle\Entity\Location
 *
 * @ORM\ManyToOne(targetEntity="Location", inversedBy="name", cascade={})
 * @ORM\JoinColumn(name="location", referencedColumnName="id")
 */
private $place;

/**
 * @var \Books\BooksBundle\Entity\Publisher
 *
 * @ORM\ManyToOne(targetEntity="Publisher", inversedBy="name", cascade={})
 * @ORM\JoinColumn(name="publisher", referencedColumnName="id")
 */
private $publisher;

//setters and getters

1 个答案:

答案 0 :(得分:0)

我在另一个实体中有一个引用OneToMany到这个导致错误的实体。我有一个拼写错误,所以引用是错误的。希望这可以保护一段时间给其他人。