duanjitong7226 2014-01-28 11:58
浏览 185
已采纳

CDbCommand无法执行SQLSTATE [23000]

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条回答 默认 最新

  • dpthuyh1678 2014-01-28 14:07
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应