Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres Pagination page() fetch one duplicate field if created_at value are the same #2575

Open
IcyTempest opened this issue Dec 19, 2023 · 0 comments

Comments

@IcyTempest
Copy link

IcyTempest commented Dec 19, 2023

Objection js Version: "3.1.3"

It seems this error happen when there are more than 50 rows of field created_at with the same value when I use page(). And it only fetched one duplicate field between page 0 and page 1 with page_size 10, and using created_at('created_at', 'desc | asc'). I'm not sure how to host this reproducible code but here's the code

/**
 *
 * install:
 *    npm install objection knex 
 *    npm install pg --save
 *
 * run:
 *    node reproduction-template
 */

let Model;

try {
  Model = require('./').Model;
} catch (err) {
  Model = require('objection').Model;
}

const Knex = require('knex');

async function main() {
  await createSchema();

  ///////////////////////////////////////////////////////////////
  // Your reproduction
  ///////////////////////////////////////////////////////////////

  const structure = () => {
    return {
      name: 'test',
    };
  };
  const rise = generateData(structure, 50);
  await RiseModel.query().insert(rise)

  for(let i =0; i<3; i++){
    const data = await getData(i);
    console.log(data.results.map(e=>e.id));
  }
  
}

async function getData(page){
  return await RiseModel.query().orderBy("created_at", "desc").page(page, 10);
}

function generateData(
  structure,
  amount
){
  const data = [];
  for (let i = 0; i < amount; i++) {
    
    data.push(structure());
  }
  return data;
}

///////////////////////////////////////////////////////////////
// Database
///////////////////////////////////////////////////////////////

const knex = Knex({
  client: "pg",
  connection: {
      host: "127.0.0.1",
      database: "Rise",
      user: // YOUR USERNAME,
      password: // YOUR PASSWORD,
  },
  useNullAsDefault: true,
  debug: false,
});

Model.knex(knex);

///////////////////////////////////////////////////////////////
// Models
///////////////////////////////////////////////////////////////

class RiseModel extends Model {
  static get tableName() {
    return "rise";
  }
}
///////////////////////////////////////////////////////////////
// Schema
///////////////////////////////////////////////////////////////

async function createSchema() {
  await knex.schema
    .dropTableIfExists('rise');
    

  await knex.schema
    .createTable('rise', table => {
      table.increments('id').primary();
      table.string('name');
      table.timestamps(true, true);
    })
}

main()
  .then(async () => {
    console.log('success');
    await knex.schema.dropTable('rise');
    return knex.destroy();
  })
  .catch(err => {
    console.error(err);
    return knex.destroy();
  });

Here's the result of id fetched:

[
  2, 3, 4,  5, 6,
  7, 8, 9, 10, 1
]
[
  12, 13, 14, 15, 16,
  17, 18, 19, 20,  1
]
[
  21, 22, 23, 24, 25,
  26, 27, 28, 29, 30
]
success
@IcyTempest IcyTempest changed the title Postgres Pagination page() fetch duplicate if created_at value are the same Postgres Pagination page() fetch one duplicate field if created_at value are the same Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant