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"}"
3
  • 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. Commented Jul 11, 2016 at 1:19
  • 5
    If you are care about the order of the keys then use json instead of jsonb type. Commented Jul 11, 2016 at 3:17
  • @Abelisto Thanks. but I think jsonb much fit my need Commented Jul 11, 2016 at 4:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.