---
title: 'This Week in #dev (Jun 16, 2023)'
teaser: 'Highlights of what happened in our #dev channel on Slack this week.'
tags: this week in dev,til,postgresql,databases,rails
author: thoughtbot
published_on: 2023-06-23
---

Welcome to another edition of This Week in #dev, a series of posts where we
bring some of the most interesting Slack conversations to the public.

## Sequence Incrementing on Postgres

A couple weeks prior, [Sara Jackson] shared some information about why FactoryBot-generated objects have perpetually incremented IDs across multiple runs of a test suite: PostgreSQL auto-increments primary keys (IDs) based on a [sequence that is created along with the table](https://www.postgresql.org/docs/current/sql-createsequence.html); it doesn't matter if the object was used just once, or for a test, or deleted – if your DB isn't reset/cleaned, the sequence still keeps track. (She also shared an article about [how to know if you're running out of IDs and what to do about it](https://www.crunchydata.com/blog/the-integer-at-the-end-of-the-universe-integer-overflow-in-postgres).)

Building on this knowledge, [Elias] and [Summer ☀️] pointed out that on Postgres, when attempting to insert a
row and it fails due to an unmet constraint, the sequence will still be
incremented. [Matheus][Matheus Richard] shares the link for the [Postgres documentation] on the
subject.

> To avoid blocking concurrent transactions that obtain numbers from the same
> sequence, the value obtained by `nextval`` is not reclaimed for re-use if the
> calling transaction later aborts. This means that transaction aborts or
> database crashes can result in gaps in the sequence of assigned values. That
> can happen without a transaction abort, too.

<aside class="warn">
  <strong>IDs are not a counter!</strong>
  <p>
    <a href="https://thoughtbot.com/blog/authors/elias-rodrigues">Elias</a>
    adds that this is why using something like <code>User.last.id</code>
    can be a way to quickly <em>estimate</em> the number of records,
    but is definitely not accurate!
  </p>
</aside>

## Active Record's `inverse_of`

[Rémy][Remy] encountered the [`inverse_of`][`inverse_of`] option in Active Record associations and was
unsure of its purpose. After some research, he found out it has two main
purposes:

1. Avoiding an extra query when fetching the record in the belongs_to
   direction
2. Helping with accepts_nested_attributes_for when creating an object
   and its children.

## Thanks

This edition was brought to you by [Elias Rodrigues][Elias], [Summer ☀️], [Matheus Richard], [Sara Jackson], and [Rémy Hannequin][Remy]. Thanks to all contributors! 🎉

[sara jackson]: https://thoughtbot.com/blog/authors/sara-jackson
[Matheus Richard]: https://thoughtbot.com/blog/authors/matheus-richard
[Postgres documentation]: https://www.postgresql.org/docs/15/functions-sequence.html
[`inverse_of`]: https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses
[Remy]: https://github.com/rhannequin
[Summer ☀️]: https://thoughtbot.com/blog/authors/summer
[Elias]: https://thoughtbot.com/blog/authors/elias-rodrigues
