Is RedBean ORM able to create unique keys?

好久不见. 提交于 2019-12-21 05:09:08

问题


I'd like RedBean to create unique keys/indexes when generating the schema. The following code does- opposed to how I understand the documentation- not do this:

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!
}

What is happening in sqlite ist this:

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

What I was expecting was this:

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

Can this be achieved without write SQL against RedBean?


回答1:


What version of Redbean are you using? It looks like they updated the buildcommand in the latest version. This is what the manual says:

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

Plugging in what you have:

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

If that doesn't work, you may have to read the actual code under the setMeta function and see what is actually going on.

To do this on an existing table it is sufficient to "store" an empty bean like this- no data needs to be added to the DB:

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

(Word of warning, if you freeze after doing this, you're not guaranteed to have all your columns)



来源:https://stackoverflow.com/questions/10687484/is-redbean-orm-able-to-create-unique-keys

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!