【问题标题】:An exception occurred while executing 'INSERT INTO proposition执行 'INSERT INTO 命题时发生异常
【发布时间】:2026-01-13 14:10:02
【问题描述】:

一个用户有多个服务并且想要添加一个服务用户。
这是我的实体:

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User extends BaseUser
{
    /**
     * @ORM\OneToMany(targetEntity="bundle\FrontBundle\Entity\Proposition", mappedBy="user", cascade={"persist"})
     * @ORM\JoinColumn(nullable=true)
     */
    protected $propositions;
    public function __construct()
    {
        $this->proposition = new ArrayCollection();

    }
}

/**
 * Proposition
 *
 * @ORM\Table(name="proposition")
 * @ORM\Entity
 */
class Proposition
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToOne(targetEntity="bundle\FrontBundle\Entity\Type", cascade={"persist", "remove"})
     * @ORM\JoinColumn(nullable=false)
     */
    protected $type;

    /**
     * @ORM\OneToOne(targetEntity="bundle\FrontBundle\Entity\NomPrestation", cascade={"persist", "remove"})
     * @ORM\JoinColumn(nullable=false)
     */
    protected $nom;

    /**
     * @ORM\ManyToOne(targetEntity="bundle\FrontBundle\Entity\User", inversedBy="propositions", cascade={"persist", "merge"})
     * @ORM\JoinColumn(nullable=false)
     */
    protected $user;

}


/**
 * Prestation
 *
 * @ORM\Table(name="type")
 * @ORM\Entity
 */
class Type
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="type", type="string", length=255)
     */
    protected $type;
}


/**
 * TypePrestation
 *
 * @ORM\Table(name="nom_prestation")
 * @ORM\Entity
 */
class NomPrestation
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=255)
     */

    protected $nom;

  }

我的问题是,当我添加第一个提案时,当我添加另一个提案时一切正常,我遇到了这个错误。

使用参数 [3442, 232, 1, 4, 1] 执行“INSERT INTO proposal (prix, prix_main_oeuvre, type_id, nom_id, user_id) VALUES (?, ?, ?, ?, ?)' 时发生异常:

SQLSTATE[23000]:违反完整性约束:1062 键“UNIQ_C7CDC353C54C8C93”的重复条目“1”

谁能帮我看得更清楚?

【问题讨论】:

  • prix、prix_main_oeuvre、type_id、nom_id、user_id 之一具有unique 约束,因此您添加了重复条目。
  • 'UNIQ_C7CDC353C54C8C93' 键用于 type_id @felipsmartins

标签: php symfony doctrine-orm


【解决方案1】:

您遇到此错误是因为 ID 为 1 的类型已存在。

我想你想多次重复使用同一个类型。如果是这样,您必须将PropositionType 之间的关系从oneToOne 更改为oneToMany

我怀疑这同样适用于您的NomPrestation 关系。如果您不重复使用这些NomPrestation,则应考虑将其添加为Proposition 的属性而不是实体。

【讨论】:

  • 谢谢你!我试试这个
  • @Darkness007 对你有帮助吗?