I have the following error appearing:
INSERT INTO GroupMembers VALUES ('Goldfrat', 'Simon Palm')
*
ERROR at line 1:
ORA-02291: integrity constraint (SHAHA1.IAM_IS_GROUP_FK) violated - parent key
not found
The constraint in the GroupMembers table is:
CONSTRAINT iam_is_group_fk FOREIGN KEY(is_group) REFERENCES Members(group_name)
The Members Table looks like this:
CREATE TABLE Members (
group_name VARCHAR2(40),
CONSTRAINT g_id_pk PRIMARY KEY(group_name),
CONSTRAINT m_group_name_fk FOREIGN KEY(group_name) REFERENCES Artist(artistic_name));
All of the tables are created fine until it comes to creating the GroupMembers table. Anyone have any ideas? I’ve been scratching for quite a while.
The problem is that
CONSTRAINT iam_is_group_fk FOREIGN KEY(is_group) REFERENCES Members(group_name); references the table Members on the group_name field.This means that the field
is_groupon the tableGroupMembersmust have an identical value on the tableMembersandgroup_namefield.In my opinion, this is bad practice.
First of all, you should have a primary key field on the table
GroupMembers. You should not store the names of the group members in the tableGroupMembers, but their corresponding id from the table Members.Also, the table Members should look something like this:
Then on the table
GroupMembers, I suppose you want to associate some members to their set of groups and back, so you should do something like this:Supposing that you have a table
Groupscontaining all the group details, with theprimary keystored asnumber, and namegroup_id.Always remember that each table must have a
primary key. It is good practice for this key to be a number.So by having
member_idinMembers,group_idinGroups, you can create a many-to-many relationship inGroupMembers. Also, put a unique index on this table so you don’t have duplicates ( the same member associated with the same id several times ).Look at this example linking users to roles. It is the same case:
