Primary key + Composite primary key causing issue in propel database schema

与世无争的帅哥 提交于 2019-12-10 22:07:50

问题


I have a table that uses an auto-incremented primary key and it has several fields.

<column name="id" type="INTEGER" primaryKey="true" required="true" autoIncrement="true" />
<column name="field1" type="INTEGER" required="true" />
<column name="field2" type="INTEGER" required="true" />
<column name="field3" type="INTEGER" />
<column name="field4" type="INTEGER" />
<column name="field5" type="INTEGER" />

I want to make sure that a field1 + field2 combo isn't used more than once, so I added them as primary keys in addition to the id, but this is creating problems when I try to use findPK(). I would prefer to have an auto-incremented id as primary key but I also want to make sure that the combo field1 + field2 isn't entered more than once.

<column name="id" type="INTEGER" primaryKey="true" required="true" autoIncrement="true" />
<column name="field1" type="INTEGER" required="true" primaryKey="true" />
<column name="field2" type="INTEGER" required="true" primaryKey="true" />

回答1:


Try setting an unique index on those fields, something like :

<unique>
  <unique-column name="/field1/" />
  <unique-column name="/field2/" />
</unique>

as per propel doc




回答2:


And here is the answer for doctrine with yaml

Pet:
  columns:
    pet_name:  {type: string(32)}
    owner_id: {type: integer}
  indexes:
    owner_name:
      fields: [pet_name, owner_id]
      type: unique


来源:https://stackoverflow.com/questions/5118941/primary-key-composite-primary-key-causing-issue-in-propel-database-schema

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