Skip to content

Instantly share code, notes, and snippets.

@thiagoszbarros
Last active August 3, 2022 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagoszbarros/87e725d54827f13dab84bda03928bae4 to your computer and use it in GitHub Desktop.
Save thiagoszbarros/87e725d54827f13dab84bda03928bae4 to your computer and use it in GitHub Desktop.
O que fazer quando a sequencia de id não está sincronizada com o valor da primarykey no postgres?

O que fazer quando a sequencia de id não está sincronizada com o valor da primarykey no postgres?

Crie uma migration com o seguinte metodo up:

    public function up()
      {
          DB::statement("SELECT setval('table_id_seq', (SELECT MAX(id) FROM table));");
      }

P: O que esse código faz?

R: Encontra o maior valor de id e atribui esse valor a sequencia do id.

P: O que é 'table_id_seq'?

R: Quando o postgre cria um campo serial, também cria uma sequecia com o padrão de nome table_column_seq.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment