Is RedBean ORM able to create unique keys?

php, redbean, sqlite

Solution

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)

Problem

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?

Original source