<div dir="ltr">I'm not surprised you're having deadlocks, I assume you're using those columns as a composite primary key which would mean the whole table is going lock while you figure out the next id in the sequence.<div><br></div><div>I wonder if it would be practical for you to just emulate this behaviour by having a regular auto increment sequential ID for the whole table and a regular foreign key to the parent then to find 69/48 you could do something like:</div><div><br></div><div>`SELECT * FROM things WHERE parent_id = 69 ORDER BY id OFFSET 47 LIMIT 1`</div><div><br></div><div>I'm sure this has plenty of issues but it would remove a bottle neck and solve a potential tricky consistency problem.</div><div><br></div><div>I hope there's not an off by one error in there :D</div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><br></div><div dir="ltr">Bestie.</div><div dir="ltr"><br><div>@thebestie</div></div></div></div>
<br><div class="gmail_quote">On 2 September 2016 at 10:11, Andrew Stewart <span dir="ltr"><<a href="mailto:boss@airbladesoftware.com" target="_blank">boss@airbladesoftware.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello LRUG!<br>
<br>
I have a Rails app using MySQL.  In the app I have a parent model with children and I would like to number the children scoped to the parent – much as GitHub issues are numbered by repo.<br>
<br>
MySQL autoincrement sequences would be ideal if they could be scoped by parent but I don't think that's possible.<br>
<br>
For the past 8 years each parent model has held a counter.  In an after_create callback in the child model, it calls a method on the parent which increments the counter and returns it; the child updates itself to store that value.<br>
<br>
This generally works but I've noticed I get the odd deadlock and uniqueness violation (there's a uniqueness constraint on the child for [parent_id, number]).  These errors crop up when people are importing large numbers of children into the parent's collection.<br>
<br>
An alternative might be to change the after_create callback to query the table for the maximum number among the parent's children, add one, then update itself with that value.  Then the parent table wouldn't be involved.  But it still feels a bit hacky.<br>
<br>
This strikes me as the sort of thing for which there must be a standard solution.  Any hints would be appreciated!<br>
<br>
Many thanks in advance,<br>
<br>
Andy Stewart<br>
______________________________<wbr>_________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>
Archives: <a href="http://lists.lrug.org/pipermail/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/<wbr>pipermail/chat-lrug.org</a><br>
Manage your subscription: <a href="http://lists.lrug.org/options.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/options.<wbr>cgi/chat-lrug.org</a><br>
List info: <a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/<wbr>listinfo.cgi/chat-lrug.org</a><br>
</blockquote></div><br></div></div>