4

I am using RedBean ORM. In order to create the schema I used the standard redbean approach of inserting data so that Redbean would auto-fit the schema to suit my needs. I put this in a script which would basically be used to build the schema when i need to initialize my database.

The problem is that RedBean keeps a row or 2 in each table (the ones that I initially inserted to get redbean to build the schema).

If it were a normal database, to erase all rows i would just drop the schema and rebuild it, but in this case that's not possible since the initial rows would still exist.

Unfortunately there isn't too much Redbean Q/A out there. Anyone know how I would do this using the Redbean interface?

I have tried

    $listOfTables = R::$writer->getTables();
foreach($listOfTables as $table)
{
    R::wipe($table);
}

Of course this doesn't work though. (It doesn't TRUNCATE the tables in the correct order so I get an error about another table using this key as a foreign link. It simply iterates in ABC order)

Fatal error: Uncaught [42000] - SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint (`redbeandb`.`research`, CONSTRAINT `research_ibfk_1` FOREIGN KEY (`ownEducationHistory_id`) REFERENCES `redbeandb`.`educationhistory` (`id`)) thrown in C:\Users\Rod\nginx-1.0.12\html\rb.php on line 105

If someone has a (redbean api) solution, it would be much appreciated. And hopefully this question can be beneficial to building up more RedBean Q/A here on Stackoverflow.

4

2 回答 2

2

利用

R::nuke();

是的,它会删除所有表,但由于 RedBeanPHP 动态创建所有表,这不是问题。

于 2012-03-21T10:08:25.837 回答
2

我知道这是一篇旧帖子,但我想我会帮助今天找到这个的人。如果您不关心数据完整性(计划擦除所有相关表),您可以告诉 mysql 忽略外键检查。

R::exec('SET FOREIGN_KEY_CHECKS = 0;');
$listOfTables = R::$writer->getTables();
foreach($listOfTables as $table)
{
    R::wipe($table);
}
R::exec('SET FOREIGN_KEY_CHECKS = 1;');
于 2017-02-21T00:15:57.950 回答