Domanda

Pretty basic stuff here, but for some reason it seems to be failing me. I have a lookup table that consists of two columns. Both columns should make up the primary key.

So the syntax for a composite key is simple enough, and I would have thought that this would have done it.

CREATE TABLE jmc_userpermissions ( 
userpermissions_permissionid int NOT NULL,
userpermissions_username varchar(55) NOT NULL,
PRIMARY KEY (userpermissions_permissionid,userpermissions_username)
)

Now the problem comes in when I try to populate.

INSERT INTO jmc_userpermissions ( userpermissions_permissionid, userpermissions_username ) VALUES ( 'updatecontact', 'angel' );
INSERT INTO jmc_userpermissions ( userpermissions_permissionid, userpermissions_username ) VALUES ( 'updateqty', 'angel' );

The second statement returns the error:

Could not insert updateqty for angel into jmc_userpermissions

Duplicate entry '0-angel' for key 'PRIMARY'

È stato utile?

Soluzione

Look at your table defintion userpermissions_permissionid int NOT NULL, now you are inserting a varchar which will be internally converted to 0. Therefore it will result in a duplicate key error on second query, since the key "0-angel" already exists after first insert. You technically insert same values into your table in both querys.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top