1

I have been facing a problem on my django application while trying to add an value to a model. It seems that there is a constraint in the DB that should not be there, according to models.py. The error I get is:

IntegrityError: null value in column "column_x_ptr_id" violates not-null constraint

Doing a \dt in psql, I get:

Indexes:
    "mytable_model_pkey" PRIMARY KEY, btree (column_x_ptr_id)
    "mytable_model_p_key" UNIQUE CONSTRAINT, btree (column_y_ptr_id)

So, my question is how can I modify this index like so?

"mytable_model_pkey" PRIMARY KEY, btree (column_y_ptr_id)

I'm not sure that would solve the problem though..

6
  • Are you saying you want to drop the current primary key, drop the unique index, and create a new primary key on column_y? Commented May 9, 2018 at 10:51
  • If this is safe, I would not mind. The fact is that we faced the problem on the prod db, so I'm a bit reluctant to do so. Good thing is that this specific table is still empty... Commented May 9, 2018 at 10:53
  • Sorry, that was a mistake... The error is "IntegrityError: null value in column "column_x_ptr_id" violates not-null constraint" Commented May 9, 2018 at 10:56
  • 2
    If column_x should allow NULL values, it cannot be a primary key. So you will have to remove the PK, and change the column to allow NULL (PK columns are defined as NOT NULL, you will have to change that). Presumably you will want to put another index on this column otherwise performance may suffer. Commented May 9, 2018 at 11:05
  • 1
    Dropping / Adding index does not modify table data. Make sure while adding a Unique / primary index, the column satisfies its constraints. Commented May 9, 2018 at 11:07

1 Answer 1

1

Ok this will give you a "more or less" of what you need to do. Your table looks something like this:

CREATE TABLE mytable_model
(
  column_x_ptr_id integer NOT NULL,
  column_y_ptr_id integer,
  CONSTRAINT mytable_model_pkey PRIMARY KEY (column_x_ptr_id),
  CONSTRAINT mytable_model_p_key UNIQUE (column_y_ptr_id)
)

You need to drop both indexes, create a new PK on the second column, and remove the NOT NULL constraint:

ALTER TABLE mytable_model DROP CONSTRAINT mytable_model_pkey;
ALTER TABLE mytable_model DROP CONSTRAINT mytable_model_p_key;
ALTER TABLE mytable_model ADD CONSTRAINT mytable_model_pkey PRIMARY KEY (column_y_ptr_id);
ALTER TABLE mytable_model ALTER COLUMN column_x_ptr_id DROP NOT NULL;

Bear in mind that adding a primary key to column_y_ptr_id will change the column to NOT NULL. If any records have NULL in that field, this will fail. Then as I mentioned, you will probably want to put another index on column_x_ptr_id for performance reasons. What type you use is up to you.

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

2 Comments

Actually, that is the table: column_y_ptr_id | integer| not null column_x_ptr_id | integer| not null value | text | As a matter of fact, as soon as the column_x_ptr_id constraint is gone, I will drop the column as well
Yep that was the drill... Thank you very much!

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.