Unique constraints and indexes for modules

68 views
Skip to first unread message

kullervo

unread,
Jun 1, 2012, 10:55:10 AM6/1/12
to ChicagoBoss
I just started my first Chicago Boss based project and got stuck while
creating my first module. What I'm trying to do is create a model
where a field is used as an unique ID which *I* choose the value for.
How do I do to make the field value both indexed (so I can query it
efficiently) and unique?

E.g. a raw SQL table with the same functionality could look like this:

CREATE TABLE appuser (
username TEXT NOT NULL,
name TEXT NOT NULL,
PRIMARY KEY (username)
);


My first try was creating this module (using the id field for the
unique username):

torhage:test martor$ cat src/model/appuser.erl
-module(appuser, [Id, Name]).
-compile(export_all).


And use it like this:

(test@torhage)2> U = appuser:new("martin", "Martin T").
{appuser,"martor","Martin T"}
(test@torhage)3> U:save().
{ok,{appuser,"martor","Martin T"}}
(test@torhage)4> boss_db:find("martor").
{appuser,"martor","Martin T"}


A problem with this approach is that the record will have the username
as BossRecord ID *without a prefix*. I.e. no namespace. Shouldn't it
automatically be prefixed with "appuser-" as when I don't specify the
ID?

I'm not used working with active record based ORMs, but for what I
know you can usually specify if a field should be unique or indexed in
the definition of the data structure.

Does CB have support for this? If not, is there a plan to support or
is there a work around?

// Martin Torhage

Evan Miller

unread,
Jul 17, 2012, 5:34:48 PM7/17/12
to chica...@googlegroups.com
Hi Martin,

Right now CB needs an "id" field and works best with auto-generated
ID. I'd like to make the system more flexible but for now that's the
convention.
--
Evan Miller
http://www.evanmiller.org/

kullervo

unread,
Aug 8, 2012, 6:52:28 PM8/8/12
to ChicagoBoss
Hi Evan,

I've got far more CB experience now so I can comment and answer my own
questions.

* The developer creates the SQL tables himself, so he has full control
in adding indexes and constraints.

* The Mock DB adapter accepts anything as the Id. The other DB
adapters I've used (PostgreSQL, MongoDB) requires the Id to be
prefixed with the module name. Hence, enforced scoping. This
difference was confusing when I started play around with Boss DB.
Maybe the Mock DB should have this requirement just for the sake of
being more alike the other DB adapters.


Btw, I really enjoy working with CB. It's nice to see the mailing list
gaining activity quickly.

// Martin Torhage

Evan Miller

unread,
Aug 9, 2012, 10:58:23 AM8/9/12
to chica...@googlegroups.com
Hi Martin,

On Wed, Aug 8, 2012 at 6:52 PM, kullervo <martin....@gmail.com> wrote:
> Hi Evan,
>
> I've got far more CB experience now so I can comment and answer my own
> questions.
>
> * The developer creates the SQL tables himself, so he has full control
> in adding indexes and constraints.
>
> * The Mock DB adapter accepts anything as the Id. The other DB
> adapters I've used (PostgreSQL, MongoDB) requires the Id to be
> prefixed with the module name. Hence, enforced scoping. This
> difference was confusing when I started play around with Boss DB.
> Maybe the Mock DB should have this requirement just for the sake of
> being more alike the other DB adapters.

That's a good point. Here's a patch that requires the "model-NN"
format, try it out:

https://github.com/evanmiller/boss_db/commit/5d0879192ffea1857075c5c05adec4f4aa8d1833
Reply all
Reply to author
Forward
0 new messages