问题
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