Unique values for two columns in Doctrine

醉酒当歌 提交于 2019-12-23 18:23:05

问题


I have entity called Dimension. It has three attributes - ID, width and height. ID is primary key. In the table, the dimension should be unique so there has to be only one record with given dimension (for example 40x30). What constraints I need to set? Is uniqueConstraints={@UniqueConstraint(name="dimension", columns={"width", "height"})} correct?


回答1:


From the documentation,

@UniqueConstraint annotation is used inside the @Table annotation on the entity-class level. It allows to hint the SchemaTool to generate a database unique constraint on the specified table columns. It only has meaning in the SchemaTool schema generation context.

Required attributes:

  • name: Name of the Index
  • columns: Array of columns.

The anwser is then YES

/**
 * @Entity
 * @Table(name="xxx",uniqueConstraints={@UniqueConstraint(name="dimension", columns={"width", "height"})})
 */
class Dimension

should then do the job.



来源:https://stackoverflow.com/questions/25810738/unique-values-for-two-columns-in-doctrine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!