Syntax error with if column on exist with primary key












0















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 ?










share|improve this question

























  • 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


















0















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 ?










share|improve this question

























  • 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
















0












0








0








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 ?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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





















  • 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



















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














2 Answers
2






active

oldest

votes


















1














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





share|improve this answer
























  • great solution, thanks

    – Laurent
    Nov 22 '18 at 17:00



















0














Try this.



DO $$ BEGIN TABLE atable ADD COLUMN IF NOT EXISTS id int primary key ; exception when others then null ; END$$;





share|improve this answer
























  • 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













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
});


}
});














draft saved

draft discarded


















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









1














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





share|improve this answer
























  • great solution, thanks

    – Laurent
    Nov 22 '18 at 17:00
















1














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





share|improve this answer
























  • great solution, thanks

    – Laurent
    Nov 22 '18 at 17:00














1












1








1







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 16:44









Kaushik NayakKaushik Nayak

18.8k41330




18.8k41330













  • 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





great solution, thanks

– Laurent
Nov 22 '18 at 17:00













0














Try this.



DO $$ BEGIN TABLE atable ADD COLUMN IF NOT EXISTS id int primary key ; exception when others then null ; END$$;





share|improve this answer
























  • 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


















0














Try this.



DO $$ BEGIN TABLE atable ADD COLUMN IF NOT EXISTS id int primary key ; exception when others then null ; END$$;





share|improve this answer
























  • 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
















0












0








0







Try this.



DO $$ BEGIN TABLE atable ADD COLUMN IF NOT EXISTS id int primary key ; exception when others then null ; END$$;





share|improve this answer













Try this.



DO $$ BEGIN TABLE atable ADD COLUMN IF NOT EXISTS id int primary key ; exception when others then null ; END$$;






share|improve this answer












share|improve this answer



share|improve this answer










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





















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin