Syntax error with if column on exist with primary key
I want to add a column which's also a primary key if it doesn't already exist on the table. If I do a simple
ALTER TABLE webinars_identities ADD COLUMN IF NOT EXISTS id uuid
It will work but if I do
ALTER TABLE webinars_identities ADD COLUMN IF NOT EXISTS id uuid PRIMARY KEY DEFAULT uuid_generate_v4();
It says it skips the alter table, but for some reason crashes right after:
NOTICE: column "id" of relation "webinars_identities" already exists, skipping
ERROR: multiple primary keys for table "webinars_identities" are not allowed
My original working query was
ALTER TABLE webinars_identities id uuid PRIMARY KEY DEFAULT uuid_generate_v4();
But this is not repeatable without error.
What am I doing wrong here ?
sql postgresql
|
show 3 more comments
I want to add a column which's also a primary key if it doesn't already exist on the table. If I do a simple
ALTER TABLE webinars_identities ADD COLUMN IF NOT EXISTS id uuid
It will work but if I do
ALTER TABLE webinars_identities ADD COLUMN IF NOT EXISTS id uuid PRIMARY KEY DEFAULT uuid_generate_v4();
It says it skips the alter table, but for some reason crashes right after:
NOTICE: column "id" of relation "webinars_identities" already exists, skipping
ERROR: multiple primary keys for table "webinars_identities" are not allowed
My original working query was
ALTER TABLE webinars_identities id uuid PRIMARY KEY DEFAULT uuid_generate_v4();
But this is not repeatable without error.
What am I doing wrong here ?
sql postgresql
The error speaks for itself. The column already exists. What is the table create statement , or are you running after the first alter table ?
– VynlJunkie
Nov 22 '18 at 15:55
That's exactly my point. I want to be able to run this query multiple times without crash, and if only the column doesn't exist, add it as primary key
– Laurent
Nov 22 '18 at 15:56
multiple primary keys for table "webinars_identities" are not allowed
is exactly what the issue is.
– 404
Nov 22 '18 at 16:01
There could be multiple unique keys in a table, but only one primary key.
– Namandeep_Kaur
Nov 22 '18 at 16:02
1
Why are you running anALTER
statement on columns with constraint rules like primary keys multiple times? Relational database tables should have entire schema and constraint rules planned and designed once in advance and not adjusted on the fly many times afterwards. This feels like an XY Problem. Please provide the fuller background of your problem and not your attempted solution.
– Parfait
Nov 22 '18 at 16:09
|
show 3 more comments
I want to add a column which's also a primary key if it doesn't already exist on the table. If I do a simple
ALTER TABLE webinars_identities ADD COLUMN IF NOT EXISTS id uuid
It will work but if I do
ALTER TABLE webinars_identities ADD COLUMN IF NOT EXISTS id uuid PRIMARY KEY DEFAULT uuid_generate_v4();
It says it skips the alter table, but for some reason crashes right after:
NOTICE: column "id" of relation "webinars_identities" already exists, skipping
ERROR: multiple primary keys for table "webinars_identities" are not allowed
My original working query was
ALTER TABLE webinars_identities id uuid PRIMARY KEY DEFAULT uuid_generate_v4();
But this is not repeatable without error.
What am I doing wrong here ?
sql postgresql
I want to add a column which's also a primary key if it doesn't already exist on the table. If I do a simple
ALTER TABLE webinars_identities ADD COLUMN IF NOT EXISTS id uuid
It will work but if I do
ALTER TABLE webinars_identities ADD COLUMN IF NOT EXISTS id uuid PRIMARY KEY DEFAULT uuid_generate_v4();
It says it skips the alter table, but for some reason crashes right after:
NOTICE: column "id" of relation "webinars_identities" already exists, skipping
ERROR: multiple primary keys for table "webinars_identities" are not allowed
My original working query was
ALTER TABLE webinars_identities id uuid PRIMARY KEY DEFAULT uuid_generate_v4();
But this is not repeatable without error.
What am I doing wrong here ?
sql postgresql
sql postgresql
edited Nov 22 '18 at 16:01
Laurent
asked Nov 22 '18 at 15:44
LaurentLaurent
1,14011326
1,14011326
The error speaks for itself. The column already exists. What is the table create statement , or are you running after the first alter table ?
– VynlJunkie
Nov 22 '18 at 15:55
That's exactly my point. I want to be able to run this query multiple times without crash, and if only the column doesn't exist, add it as primary key
– Laurent
Nov 22 '18 at 15:56
multiple primary keys for table "webinars_identities" are not allowed
is exactly what the issue is.
– 404
Nov 22 '18 at 16:01
There could be multiple unique keys in a table, but only one primary key.
– Namandeep_Kaur
Nov 22 '18 at 16:02
1
Why are you running anALTER
statement on columns with constraint rules like primary keys multiple times? Relational database tables should have entire schema and constraint rules planned and designed once in advance and not adjusted on the fly many times afterwards. This feels like an XY Problem. Please provide the fuller background of your problem and not your attempted solution.
– Parfait
Nov 22 '18 at 16:09
|
show 3 more comments
The error speaks for itself. The column already exists. What is the table create statement , or are you running after the first alter table ?
– VynlJunkie
Nov 22 '18 at 15:55
That's exactly my point. I want to be able to run this query multiple times without crash, and if only the column doesn't exist, add it as primary key
– Laurent
Nov 22 '18 at 15:56
multiple primary keys for table "webinars_identities" are not allowed
is exactly what the issue is.
– 404
Nov 22 '18 at 16:01
There could be multiple unique keys in a table, but only one primary key.
– Namandeep_Kaur
Nov 22 '18 at 16:02
1
Why are you running anALTER
statement on columns with constraint rules like primary keys multiple times? Relational database tables should have entire schema and constraint rules planned and designed once in advance and not adjusted on the fly many times afterwards. This feels like an XY Problem. Please provide the fuller background of your problem and not your attempted solution.
– Parfait
Nov 22 '18 at 16:09
The error speaks for itself. The column already exists. What is the table create statement , or are you running after the first alter table ?
– VynlJunkie
Nov 22 '18 at 15:55
The error speaks for itself. The column already exists. What is the table create statement , or are you running after the first alter table ?
– VynlJunkie
Nov 22 '18 at 15:55
That's exactly my point. I want to be able to run this query multiple times without crash, and if only the column doesn't exist, add it as primary key
– Laurent
Nov 22 '18 at 15:56
That's exactly my point. I want to be able to run this query multiple times without crash, and if only the column doesn't exist, add it as primary key
– Laurent
Nov 22 '18 at 15:56
multiple primary keys for table "webinars_identities" are not allowed
is exactly what the issue is.– 404
Nov 22 '18 at 16:01
multiple primary keys for table "webinars_identities" are not allowed
is exactly what the issue is.– 404
Nov 22 '18 at 16:01
There could be multiple unique keys in a table, but only one primary key.
– Namandeep_Kaur
Nov 22 '18 at 16:02
There could be multiple unique keys in a table, but only one primary key.
– Namandeep_Kaur
Nov 22 '18 at 16:02
1
1
Why are you running an
ALTER
statement on columns with constraint rules like primary keys multiple times? Relational database tables should have entire schema and constraint rules planned and designed once in advance and not adjusted on the fly many times afterwards. This feels like an XY Problem. Please provide the fuller background of your problem and not your attempted solution.– Parfait
Nov 22 '18 at 16:09
Why are you running an
ALTER
statement on columns with constraint rules like primary keys multiple times? Relational database tables should have entire schema and constraint rules planned and designed once in advance and not adjusted on the fly many times afterwards. This feels like an XY Problem. Please provide the fuller background of your problem and not your attempted solution.– Parfait
Nov 22 '18 at 16:09
|
show 3 more comments
2 Answers
2
active
oldest
votes
Handle it using duplicate_column
exception and issue a notice, because someone rightly said that errors should never pass silently.
DO $body$
BEGIN
ALTER TABLE atable ADD COLUMN id int primary key; --DEFAULT uuid_generate_v4()
EXCEPTION
WHEN duplicate_column THEN
RAISE NOTICE 'ERROR: %,%',SQLSTATE,SQLERRM;
END $body$;
This will work the first time and does not fail on all following attempts, but gives you a message. All other errors if found in your statement will be raised as exceptions.
NOTICE: ERROR: 42701,column "id" of relation "atable" already exists
DO
great solution, thanks
– Laurent
Nov 22 '18 at 17:00
add a comment |
Try this.
DO $$ BEGIN TABLE atable ADD COLUMN IF NOT EXISTS id int primary key ; exception when others then null ; END$$;
This will not throw exception for any run time error in the statement and will silence the genuine errors.
– Kaushik Nayak
Nov 22 '18 at 16:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434366%2fsyntax-error-with-if-column-on-exist-with-primary-key%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Handle it using duplicate_column
exception and issue a notice, because someone rightly said that errors should never pass silently.
DO $body$
BEGIN
ALTER TABLE atable ADD COLUMN id int primary key; --DEFAULT uuid_generate_v4()
EXCEPTION
WHEN duplicate_column THEN
RAISE NOTICE 'ERROR: %,%',SQLSTATE,SQLERRM;
END $body$;
This will work the first time and does not fail on all following attempts, but gives you a message. All other errors if found in your statement will be raised as exceptions.
NOTICE: ERROR: 42701,column "id" of relation "atable" already exists
DO
great solution, thanks
– Laurent
Nov 22 '18 at 17:00
add a comment |
Handle it using duplicate_column
exception and issue a notice, because someone rightly said that errors should never pass silently.
DO $body$
BEGIN
ALTER TABLE atable ADD COLUMN id int primary key; --DEFAULT uuid_generate_v4()
EXCEPTION
WHEN duplicate_column THEN
RAISE NOTICE 'ERROR: %,%',SQLSTATE,SQLERRM;
END $body$;
This will work the first time and does not fail on all following attempts, but gives you a message. All other errors if found in your statement will be raised as exceptions.
NOTICE: ERROR: 42701,column "id" of relation "atable" already exists
DO
great solution, thanks
– Laurent
Nov 22 '18 at 17:00
add a comment |
Handle it using duplicate_column
exception and issue a notice, because someone rightly said that errors should never pass silently.
DO $body$
BEGIN
ALTER TABLE atable ADD COLUMN id int primary key; --DEFAULT uuid_generate_v4()
EXCEPTION
WHEN duplicate_column THEN
RAISE NOTICE 'ERROR: %,%',SQLSTATE,SQLERRM;
END $body$;
This will work the first time and does not fail on all following attempts, but gives you a message. All other errors if found in your statement will be raised as exceptions.
NOTICE: ERROR: 42701,column "id" of relation "atable" already exists
DO
Handle it using duplicate_column
exception and issue a notice, because someone rightly said that errors should never pass silently.
DO $body$
BEGIN
ALTER TABLE atable ADD COLUMN id int primary key; --DEFAULT uuid_generate_v4()
EXCEPTION
WHEN duplicate_column THEN
RAISE NOTICE 'ERROR: %,%',SQLSTATE,SQLERRM;
END $body$;
This will work the first time and does not fail on all following attempts, but gives you a message. All other errors if found in your statement will be raised as exceptions.
NOTICE: ERROR: 42701,column "id" of relation "atable" already exists
DO
answered Nov 22 '18 at 16:44
Kaushik NayakKaushik Nayak
18.8k41330
18.8k41330
great solution, thanks
– Laurent
Nov 22 '18 at 17:00
add a comment |
great solution, thanks
– Laurent
Nov 22 '18 at 17:00
great solution, thanks
– Laurent
Nov 22 '18 at 17:00
great solution, thanks
– Laurent
Nov 22 '18 at 17:00
add a comment |
Try this.
DO $$ BEGIN TABLE atable ADD COLUMN IF NOT EXISTS id int primary key ; exception when others then null ; END$$;
This will not throw exception for any run time error in the statement and will silence the genuine errors.
– Kaushik Nayak
Nov 22 '18 at 16:52
add a comment |
Try this.
DO $$ BEGIN TABLE atable ADD COLUMN IF NOT EXISTS id int primary key ; exception when others then null ; END$$;
This will not throw exception for any run time error in the statement and will silence the genuine errors.
– Kaushik Nayak
Nov 22 '18 at 16:52
add a comment |
Try this.
DO $$ BEGIN TABLE atable ADD COLUMN IF NOT EXISTS id int primary key ; exception when others then null ; END$$;
Try this.
DO $$ BEGIN TABLE atable ADD COLUMN IF NOT EXISTS id int primary key ; exception when others then null ; END$$;
answered Nov 22 '18 at 16:11
VynlJunkieVynlJunkie
773815
773815
This will not throw exception for any run time error in the statement and will silence the genuine errors.
– Kaushik Nayak
Nov 22 '18 at 16:52
add a comment |
This will not throw exception for any run time error in the statement and will silence the genuine errors.
– Kaushik Nayak
Nov 22 '18 at 16:52
This will not throw exception for any run time error in the statement and will silence the genuine errors.
– Kaushik Nayak
Nov 22 '18 at 16:52
This will not throw exception for any run time error in the statement and will silence the genuine errors.
– Kaushik Nayak
Nov 22 '18 at 16:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434366%2fsyntax-error-with-if-column-on-exist-with-primary-key%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The error speaks for itself. The column already exists. What is the table create statement , or are you running after the first alter table ?
– VynlJunkie
Nov 22 '18 at 15:55
That's exactly my point. I want to be able to run this query multiple times without crash, and if only the column doesn't exist, add it as primary key
– Laurent
Nov 22 '18 at 15:56
multiple primary keys for table "webinars_identities" are not allowed
is exactly what the issue is.– 404
Nov 22 '18 at 16:01
There could be multiple unique keys in a table, but only one primary key.
– Namandeep_Kaur
Nov 22 '18 at 16:02
1
Why are you running an
ALTER
statement on columns with constraint rules like primary keys multiple times? Relational database tables should have entire schema and constraint rules planned and designed once in advance and not adjusted on the fly many times afterwards. This feels like an XY Problem. Please provide the fuller background of your problem and not your attempted solution.– Parfait
Nov 22 '18 at 16:09