tags:

views:

334

answers:

3

I want to do something like this:


create table app_users
(
    app_user_id smallint(6) not null auto_increment primary key,
    api_key     char(36) not null default uuid()
);

However this results in a error, is there a way to call a function for a default value in mysql?

thanks.

+3  A: 

Unfortunately no, MySQL 5 requires constants for the default. The issue was discussed in much more detail in the link below. But the only answer is to allow null and add a table trigger.

MySQL only recently accepted UUID as part of their DB package, and it's not as feature rich as we'd like.

http://www.phpbuilder.com/board/showthread.php?t=10349169

TravisO
+7  A: 

No, you can't.

However, you could easily create a trigger to do this, such as:

CREATE TRIGGER before_insert_app_users
  BEFORE INSERT ON app_users 
  FOR EACH ROW
  SET new.api_key = uuid();
Harrison Fisk
Thanks, that was just what I needed.
Jarett
+2  A: 

I believe you can't:

the default value must be a constant; it cannot be a function or an expression

Thibaut Barrère