openlang

Removing an optional column safely with PostgreSQL 9+

The conversation continues 2

Follow the reasoning
  1. Permalink

    Simply include IF EXIST in the statement you're using for DROP COLUMN:

    ALTER TABLE targetTable DROP COLUMN
    IF EXISTS targetColumn;
  2. Permalink

    Another option is the IF EXISTS approach, which works nicely when we're performing a migration

    DO $$
    BEGIN
      IF EXISTS (
        SELECT column_name
        FROM information_schema.columns
        WHERE table_name = targetTable AND column_name = targetColumn
      ) THEN
        ALTER TABLE targetTable DROP COLUMN targetColumn;
      END IF;
    END $$;

Other sides of the same question

  1. Missing field through a derived table in SQL

    I'm executing the following SQL statement: It produces the following error: #1054 - Unknown column 'shipping.post_id' in 'on…

    Replies 2Latest activity sql
  2. Testing D arrays for a matching entry

    with associative arrays this is possible how is this done with ordinary arrays? I'd like a validation expression, for example

    Replies 2Latest activity d
  3. Solc 0.7.0 rejects construction of my struct with a mapping member

    My npm installation provides Solc version 0.7.0. Creating a Struct with a mapping inside emits the message: "Struct containin…

    Replies 2Latest activity solidity