Reputation: 2022
I'm trying to create a user but every time click to create, I get this error:
CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`dcpcommunity`.`system_users`, CONSTRAINT `system_users_ibfk_1` FOREIGN KEY (`party_id`) REFERENCES `parties` (`id`)). The SQL statement executed was: INSERT INTO `system_users` (`status`, `date_modified`, `username`, `password`, `date_last_login`, `date_created`, `user_role`, `isLogin`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6, :yp7)
I read that this error means that I do not have a 'party_id' in my table, but I triple checked and party_id exists as my primary key for my system_users table.
I also see that it says REFERENCES parties
and I checked in my model the references part and changed it to my table name and I still get this error.
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'party' => array(self::BELONGS_TO, 'system_users', 'party_id'),
);
}
EDIT
[system_users]
pk: party_id
username
password
date_last_login
status
date_created
date_modified
user_role
isLogin
There is a Parties table with fields id and party_type_id
EDIT
I checked my db and In my system_users table, I found that party_id has a relation to :
db.parties.id
How do I make this relationship possible when creating a new record?
Upvotes: 0
Views: 427
Reputation: 391
as the sql error states: REFERENCES parties id... parties is a table in the db. AR will look for a AR model called Party, as the relations function states. In the AR model "party" should be a definition where it uses the table "parties". The error comes from the the db (schema). The create action tries to insert a value which doesnt correspond to the schema definitions in system_users or parties.
Upvotes: 1