簡體   English   中英

RedBean ORM能夠創建唯一鍵嗎?

[英]Is RedBean ORM able to create unique keys?

我希望RedBean在生成模式時創建唯一的鍵/索引。 以下代碼確實與我理解文檔的方式相反 - 不是這樣做的:

ř::設置( '源碼: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);

(警告,如果你這樣做后凍結,你不能保證你的所有列都有)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM