RedBean ORM是否能够创建唯一的密钥


Is RedBean ORM able to create unique keys?

我希望RedBean在生成模式时创建唯一的键/索引。以下代码 - 与我理解文档的方式相反 - 不这样做:

R::

setup('sqlite:rss_loader.db3'(;

$bean = R::findOne(IMG);
if (!$bean->id) {
    $bean = R::dispense(IMG);
    $bean->setMeta("buildcommand.unique.0", array('url'));
    $bean->url      = 'text';
    R::store($bean);
    $bean->wipe();
    R::freeze(); //no more schema changes!
}

sqlite 中发生了什么:

create table img (id integer primary key autoincrement, url) 

我期待的是:

create table img (id integer primary key autoincrement, url text unique) 

这可以在不针对RedBean编写SQL的情况下实现吗?

你使用的是哪个版本的Redbean? 看起来他们在最新版本中更新了buildcommand。 手册是这样说的:

$bean->setMeta("buildcommand.unique" , array(array($property1, $property2)));

插入您拥有的内容:

$bean->setMeta("buildcommand.unique" , array(array('url')));

如果这不起作用,您可能需要阅读setMeta函数下的实际代码,看看实际发生了什么。

要在现有表上执行此操作,像这样"存储"一个空 bean 就足够了 - 无需将任何数据添加到数据库中:

$bean = R::dispense(IMG);
$bean->setMeta("buildcommand.unique", array(array(...)));
R::store($bean);

(警告的话,如果你在这样做后冻结,你不能保证你所有的列(