Yii Framework Forum: [ASK]Error Foreign Key - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[ASK]Error Foreign Key Rate Topic: -----

#1 User is offline   yusronnube 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 30-November 10

Posted 09 January 2011 - 10:24 PM

mau nanya nih gan,
kalo error kyk gini knp yah?


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 (`alumni2/al_aktifitas_alumni`, CONSTRAINT `fk_aktifitas_alumni_1` FOREIGN KEY (`idaktifitas`) REFERENCES `al_aktifitas` (`idaktifitas`) ON DELETE NO ACTION ON UPDATE CASCADE)




function relation di model biodata.php
        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(
                        'aktifitasAlumnis' => array(self::HAS_MANY, 'AktifitasAlumni', 'idalumni'),
                        'aktifitas' => array(self::MANY_MANY, 'Aktifitas', 'idaktifitas'),
                        'idprogram_studi'=>array(self::BELONGS_TO,'program_studi','idprogram'),
                );
        }

maklum newbie bgt :)
0

#2 User is offline   rei 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 318
  • Joined: 10-November 10

Posted 15 January 2011 - 01:31 AM

View Postyusronnube, on 09 January 2011 - 10:24 PM, said:

mau nanya nih gan,
kalo error kyk gini knp yah?


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 (`alumni2/al_aktifitas_alumni`, CONSTRAINT `fk_aktifitas_alumni_1` FOREIGN KEY (`idaktifitas`) REFERENCES `al_aktifitas` (`idaktifitas`) ON DELETE NO ACTION ON UPDATE CASCADE)




function relation di model biodata.php
        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(
                        'aktifitasAlumnis' => array(self::HAS_MANY, 'AktifitasAlumni', 'idalumni'),
                        'aktifitas' => array(self::MANY_MANY, 'Aktifitas', 'idaktifitas'),
                        'idprogram_studi'=>array(self::BELONGS_TO,'program_studi','idprogram'),
                );
        }

maklum newbie bgt :)



Keliatannya sih gagal delete record di tabel yang terelasi ama dia, gan. Soalnya untuk reference option delete-nya diset 'NO ACTION'. Klo ga salah di MySQL 'NO ACTION' artinya jadi 'RESTRICT'
(http://dev.mysql.com...onstraints.html).

Solusinya mungkin di option buat ON DELETE-nya dibikin jadi 'CASCADE' juga ato 'SET NULL'. Tapi itu tergantung keinginan agan juga sih hasilnya mo kaya gimana ... :)
0

#3 User is offline   nasrul 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 98
  • Joined: 14-April 09
  • Location:Sorowako - Indonesia

Posted 16 January 2011 - 11:04 AM

kurang yakin sih, tapi mungkin parameter foreignkey untuk relasi 'aktifitas'-nya ada yang kurang.
coba dengan ini:
...
'aktifitas' => array(self::MANY_MANY, 'Aktifitas', 'AktifitasAlumni(idalumni,idaktifitas)'),
...


soalnya dalam relasi many_many, biasanya diperlukan sebuah tabel yang memiliki field yang terhubung sebagai ForeignKey (FK) ke masing-masing tabel.
misal:
[biodata] 1---m [AktifitasAlumni] m--1 [aktifitas]
- idAlumni (PK) - idAlumni (FK) - idAktifitas {PK}
- idAktifitas {FK}
0

#4 User is offline   aqge 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 188
  • Joined: 02-September 10
  • Location:Indonesia

Posted 18 January 2011 - 05:19 AM

gan mending gak usah pake innodb gan... myisam saja deh.... saya sudha 4 bulan meningglkan innoDB and im a happy person hehehe... (IMHO)
Best Regard
Tidak ada coding yang salah
Yang ada hanyalah Coding yang lebih baik
0

#5 User is offline   -=YiieRz=- 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 177
  • Joined: 27-October 10
  • Location:Jakarta

Posted 20 January 2011 - 03:41 AM

View Postaqge, on 18 January 2011 - 05:19 AM, said:

gan mending gak usah pake innodb gan... myisam saja deh.... saya sudha 4 bulan meningglkan innoDB and im a happy person hehehe... (IMHO)


setuju gan, myISAM lbh jarang error pas di export import ^^ (pengalaman lama sih ... hehehe).
kalo bedanya sih kurang jelas ane .... kurang makan bangku sekolahan plajaran inno sama isam ane neh gan ... hehehehe.


Quote

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 (`alumni2/al_aktifitas_alumni`, CONSTRAINT `fk_aktifitas_alumni_1` FOREIGN KEY (`idaktifitas`) REFERENCES `al_aktifitas` (`idaktifitas`) ON DELETE NO ACTION ON UPDATE CASCADE)


kemungkinan besar sih ada 2 (menurut ane)
1. dbTypenya di set NOT NULL ........ tapi pas di insert tidak ada valuenya.
2. dbTypenya di set NOT NULL PRIMARY KEY .......... tapi pas di insert valuenya sudah ada ada value yg sama sebelumnya (redundant)

kalo mo yakin mendingan di check dulu sebelum insert ... pake print_r($varnyaApa);
^^
belajar itu modal utama, tanpa percobaan itu tak akan berhasil. berdoa itu membuat kita percaya diri. YiieRz-SdgBlajar
www.yiiframework.com
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users