0

I am using a temp table to hold some data while I tidy up a database table. Narrowed down, it goes something like this:

CREATE TEMP TABLE fw_temp AS SELECT time, version, device_id FROM fw_status;
DELETE FROM fw_status;
INSERT INTO fw_status SELECT * FROM fw_temp

On my INSERT statement I get some.. interesting behaviour when the values in the device_id column are null:

SQL Error [42804]: ERROR: column "device_id" is of type bigint but expression is of type text Hint: You will need to rewrite or cast the expression.

As the error message may lead on, device_id is indeed bigint type in the table, but appears to have lost the type when we select from the temp table.

Making a general select,

CREATE TEMP TABLE fw_temp AS SELECT * FROM fw_status;
DELETE FROM fw_status;
INSERT INTO fw_status SELECT * FROM fw_temp

interestingly does not cause this error.

Now, the real temp table is a bit more complex, so sadly going with SELECT * is not an option. How can I go about this error?

3
  • 1
    Maybe INSERT INTO fw_status (time, version, device_id) SELECT * FROM fw_temp;. Misplaced and missing column values? Commented Oct 21, 2022 at 11:56
  • Funny thing, I figured the same and saw your comment while posting the fix. Any idea what happens behind the scene? I notice column order does not appear to be maintained on the general select Commented Oct 21, 2022 at 12:01
  • You were trying to insert into the wrong columns of fw_status - the first three - regardless of what column names the select statement is producing. Commented Oct 21, 2022 at 12:04

1 Answer 1

0

Changing to

CREATE TEMP TABLE fw_temp AS SELECT time, version, device_id FROM fw_status;
DELETE FROM fw_status;
INSERT INTO fw_status (time, version, device_id) SELECT time, version, device_id FROM fw_temp

resolved the issue. Apparently, you can mix general select and general insert or specific select and specific insert, but not general select and specific insert. I am still puzzled as to what is going on. Any enlightenment would be appreciated

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.