所以這個項目正在將一些直接的PDO SQL查詢(使用MySQL)轉換爲Doctrine。這些表格已經存在並且有數據。我設立了這些實體,並且一切看起來都很棒。所有的查詢都在Doctrine中的實體管理器中重寫,並且一切似乎都奏效。Symfony2 + Doctrine2 SQL完整性違規:可空字段不能爲空?
除了我忘記了其中一個實體中的三個字段需要可以爲空。它們在數據庫中設置爲NOT NULL,並且在Doctrine註釋中沒有nullable = true聲明。
我手動改變,以表在MySQL使字段爲空,然後添加一個可空=真到了實體,清除緩存和執行的
php app/console doctrine:schema:update --force
只是可以肯定(這正確執行)。
然而,儘管學說實體允許空字段,數據庫中有列可爲空爲好,我仍然得到這個錯誤:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'type_id' cannot be null
有沒有辦法對我來說,不解決這個問題刪除表(這將涉及保存所有的數據)?這到底是什麼錯誤?根據所涉及的所有代碼和數據庫,根本不應該有完整性違規(該領域也不是外鍵)。
這裏是有問題的實體:
class Audio
{
/**
* @ORM\Id
* @ORM\Column(type="integer", name="id")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", name="type", columnDefinition="ENUM('song','demo')")
*/
protected $type;
/**
* @ORM\Column(type="integer", name="type_id", nullable=true)
*/
protected $typeId = null;
/**
* @ORM\Column(type="string", name="s3_url", nullable=true)
*/
protected $s3url = null;
/**
* @ORM\Column(type="string", name="s3_key", nullable=true)
*/
protected $s3key = null;
/**
* @ORM\Column(type="datetime", name="date_created")
*/
protected $dateCreated;
/**
* @ORM\Column(type="datetime", name="date_modified")
*/
protected $dateModified;
這絕對是神祕我。我真的沒有更多的方法可以告訴程序它可以爲空。
您是否緩存緩存?如果是這樣,請確保您的模式確實如您所期望的那樣(使用:「show create table Audio」)。順便說一下,不要在數據庫中手動進行更改 - 讓教條去做。 – Cyprian
是的,我清除了緩存(正如我在文章中提到的那樣)並且我也運行了架構更新。 – JRL
好的,當你運行「show create table audio」(從mysql控制檯)時,type_id列旁邊沒有「not null」嗎? – Cyprian