0

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?

1
  • 1
    Could you please show us the scheme of your party and system users schema? Commented Jan 28, 2014 at 12:59

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to make it link towards the table I want it to insert into? I generated these models through Gii so I'm not sure why it's linking to the wrong table.
What do you meam with "linking towards the table" ?
What I meant was passing the model from the controller to the view (create).
you have to extend the create action yiiframework.com/wiki/19/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.