On Wednesday, May 24, 2017 at 10:37:58 PM UTC-7, Hidetaka Matsumoto wrote:
>
> Hi,
>
> Is there any way to use Sequel::Model.max in Sequel::Model.create?
>
> I want the query like bellow.
> INSERT INTO users (name,number) VALUES ('hoo',(SELECT MAX(number)+1 FROM
> users));
>
> Because I want to increment the number atomically.
>
As a general rule, Sequel::Model is designed for values, not for
expressions. Use the dataset layer to insert with expressions. There are
various ways to get expressions to work with models, but it's really a
better approach to use the dataset layer.
For the specific example you gave, you are really better off using a
database sequence as a default value for users.number, or a similar
approach to automatically increment the users.number column, and not using
a max function. I'm guessing on many databases the query you gave will
fail when run concurrently if you have a unique index on users.number.
Thanks,
Jeremy.
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.