Home Cannot truncate a table referenced in a foreign key constraint in Codeigniter
Reply: 0

Cannot truncate a table referenced in a foreign key constraint in Codeigniter

user895
1#
user895 Published in May 25, 2018, 6:55 pm

i'm making a DB seeder using CodeIgniter

here is the code

class AreasSeeder extends Seeder {

private $table = 'areas';

public function run() {
    $this->db->truncate($this->table);

    $data = [
        [
            'name' => 'area1'
        ],
        [
            'name' => 'area2'
        ]
    ];

    $this->db->insert_batch($this->table, $data);

    echo PHP_EOL;
    }
}

when it run this display error

Error Number: 1701
    Cannot truncate a table referenced in a foreign key constraint (`myproject`.`area_pref_relations`, CONSTRAINT `area_pref_relations_ibfk_1` FOREIGN KEY (`area_id`) REFERENCES `myproject`.`areas` (`id`))
    TRUNCATE `areas`
    Filename: database/seeds/areaSeeder.php
    Line Number: 8

do i need to reset all table related on table areas?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304274 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO