5

When I save data into table , then do select from table it become different order, I check in pgadmin it is too.
Why?? and how to solve it?

CREATE TABLE IF NOT EXISTS "user_role_track"(
    "id" SERIAL NOT NULL,
    "create_date" timestamp without time zone,
    "create_by_user_id" integer,
    "action" integer,
    "old_data" jsonb,
    "new_data" jsonb
  );

data create in nodejs app

var newData = {
  "id": userRoleId,
  "create_date": timestamp,
  "user_id": userId,
  "role": role
};
... 

// save with promise sync function
var dbQueryR = yield Promise.resolve( queryPromise(dbClient, dbQuery, dbParams) );

select from table/ see through pgadmin

"{"id": 2, "role": 1, "user_id": 17, "create_date": "2016-07-11 09:09:18"}"
user1775888
  • 3,147
  • 13
  • 45
  • 65
  • 1
    there is no guarantee in the order of the data stored due to the optimisations made by the DBMS. If u need a particular order, you should use `ORDER BY` to fetch the result. – mauris Jul 11 '16 at 01:19
  • 4
    If you are care about the order of the keys then use `json` instead of `jsonb` type. – Abelisto Jul 11 '16 at 03:17
  • @Abelisto Thanks. but I think jsonb much fit my need – user1775888 Jul 11 '16 at 04:35

1 Answers1

3

If I understand you rigtht, your json elements are mixed up after insert?..

then look at json specs

An object is an unordered set of name/value pairs

as stated in JSON order mixed up

You cannot and should not rely on the ordering of elements within a JSON object.

Community
  • 1
  • 1
Vao Tsun
  • 47,234
  • 13
  • 100
  • 132