SPOD unique field

97 views
Skip to first unread message

kevin...@gmail.com

unread,
Oct 7, 2013, 4:48:40 AM10/7/13
to haxe...@googlegroups.com
Hi,

Are there any ways to specify a field to be unique using SPOD? And will SPOD throw any errors when insert() duplicated values in a unique field?

Thanks.
Kevin

Michel Romecki

unread,
Oct 7, 2013, 5:01:07 AM10/7/13
to Haxe ML
Hi,
I think you can specify that using @:index()


2013/10/7 <kevin...@gmail.com>

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/groups/opt_out.

postite

unread,
Oct 7, 2013, 6:20:13 AM10/7/13
to haxe...@googlegroups.com

AFAIK @:index() is useful but does not throw any errors ( runtime stuff )…

Jason O'Neil

unread,
Oct 7, 2013, 6:27:03 AM10/7/13
to haxe...@googlegroups.com

If you use Nicolas' dbadmin then the @:index metadata creates the necessary constraints on your MySQL tables.

If you have those constraints (you can also add them manually or with phpmyadmin also) then you get runtime errors you can catch if there are duplicate records

postite

unread,
Oct 7, 2013, 7:55:52 AM10/7/13
to haxe...@googlegroups.com
sweet !

Eric Priou

unread,
Oct 7, 2013, 10:41:31 AM10/7/13
to haxe...@googlegroups.com
H
i,
> If you use Nicolas' dbadmin then the @:index metadata creates the necessary constraints on your MySQL tables.
>
dbadmin is neko only, ain't it ?

Anyway the way to use it is not clear to me :
"map sys.db.Admin.handler() to "/db" URL on your website"

here is the method code :
public static function handler() {
Manager.initialize(); // make sure it's been done
try {
new Admin().process();
} catch( e : Dynamic ) {
// rollback in case of multiple delete/update - no effect on DB struct changes
// since they are done outside of transaction
Manager.cnx.rollback();
neko.Lib.print("<pre>");
neko.Lib.print(Std.string(e));
neko.Lib.print(haxe.CallStack.toString(haxe.CallStack.exceptionStack()));
neko.Lib.print("</pre>");
}
}

Thanks for any help.

---
Eric Priou aka erixtekila
http://www.ericpriou.net

Jason O'Neil

unread,
Oct 7, 2013, 10:23:49 PM10/7/13
to haxe...@googlegroups.com
Hi Eric

You are correct, currently dbadmin is tied to neko and mysql:
  • The neko dependency is very loose - I think porting it to PHP would be very straight forward, and I plan to do this at some point... I have not been able to yet though :)  A NodeJS target would be more difficult, not least because Haxe's DB macros don't work asynchronously at the moment :)

  • The mysql dependency - I'm not sure how strong it is, I have not tested it with Sqlite or a different database driver.  Nicolas may be able to comment more on what the limitations there are.

For usage:

  • DB Admin is sadly hard coded to expect to be run from "/db/" - for example "jasononeil.com.au/db/" (I do have a subclass which lets you specify a different path if you are interested).

  • The easiest way to do it may be to create an "index.n" and put it in a "db" subfolder, using .htaccess or similar to password protect.

  • The other way, if you are using a MVC system, is to define a route to "/db", and have the controller/action for that route execute the `handler()`.  If you are using Dispatch, that would be "doDb() { sys.db.Admin.handler(); }".  This function does it's own templating and path handling etc, so if you are using something like ufront that expects a return value, you will need to make sure you return an empty result that doesn't overwrite what DBAdmin has written.

As a fallback, you can always use TableCreate to set up your tables, and a tool like phpmyadmin to add the constraints manually, though DBAdmin does detect changes to your models and offer to make those changes to the database, which is pretty handy.

Jason




Nicolas Cannasse

unread,
Oct 8, 2013, 4:19:50 AM10/8/13
to haxe...@googlegroups.com
Le 08/10/2013 04:23, Jason O'Neil a �crit :
> Hi Eric
>
> You are correct, currently dbadmin is tied to neko and mysql:
>
> * The neko dependency is very loose - I think porting it to PHP would
> be very straight forward, and I plan to do this at some point... I
> have not been able to yet though :) A NodeJS target would be more
> difficult, not least because Haxe's DB macros don't work
> asynchronously at the moment :)
>
> * The mysql dependency - I'm not sure how strong it is, I have not
> tested it with Sqlite or a different database driver. Nicolas may
> be able to comment more on what the limitations there are.

There's several issues with Sqlite, such as no support for Date. I
haven't played a lot with it so I think it could be improved.

We could indeed make dbadmin cross platform / cross database without
much efforts. I have no time for it but I'm accepting pull requests.

Best,
Nicolas

kevin...@gmail.com

unread,
Jan 13, 2014, 9:47:21 AM1/13/14
to haxe...@googlegroups.com
Looking at the Docs again:

  • @:index(field1,field2,...,[unique]) : declare an index consisting of the specified classes fields - in that order. If the last field is unique then it means that's an unique index (each combination of fields values can only occur once)
Does that mean i put the keyword "unique" at the end or a field which is unique?

Jason O'Neil

unread,
Jan 13, 2014, 9:01:49 PM1/13/14
to haxe...@googlegroups.com
Some example usage...

To make an index on the field "email", and force it to be unique:

@:index(email,unique)
class User {
    public var email:SString<255>;
    public var passwordHash:SString<255>;
}

To make an index on the fields "date" AND "roomID", so that you can never have a booking in the same room on the same day:

@:index(date,roomID,unique)
class Booking {
    public var date:SDate;
    public var roomID:SUInt;
}

Hope that helps :)  As mentioned before, when you create an index with metadata, the "sys.db.TableCreate" class ignores it, but if you use the "dbadmin" haxelib it will enforce these rules in MySQL databases.

Jason


--
Reply all
Reply to author
Forward
0 new messages