Adding a unique constraint to a table

58 views
Skip to first unread message

Kevin Lyda

unread,
Jan 14, 2016, 4:19:55 AM1/14/16
to redbeanphp
I find references to this in the old version 3 docs, but nothing in the current ones (might just be my poor searching). In the old docs, the way described to add a constraint was to do this:

$u = R::dispense('user');
$u->name = 'schema setting';
$u->email = 'schema setting';
$u->pwd = password_hash('schema setting', PASSWORD_DEFAULT);
$u->setMeta('buildcommand.unique', array('email'));
$id = R::store($u);
R::trash($u);

But if I look at the schema before and after I don't see UNIQUE on the email column:

sqlite3 $(git rev-parse --show-toplevel)/test.db '.schema user'
CREATE TABLE `user` ( id INTEGER PRIMARY KEY AUTOINCREMENT , `name` TEXT, `email` TEXT, `pwd` TEXT);

I also tried $u->setMeta('sys.buildcommand.unique', array('email')); but that didn't work either.

Note that this is with sqlite3 and that the table already existed, I'm just trying to add the constraint now.

Kevin

Davide Gavagnin

unread,
Aug 31, 2018, 6:56:39 AM8/31/18
to redbeanphp
Found the way on the latest release:

R::getWriter()->addUniqueConstraint(nameofthebean, arrayofcolumnnames);
Reply all
Reply to author
Forward
0 new messages