Remove duplicate rows in table with dependent foreign keysHow to remove duplicate rows with foreign keys dependencies?Removing duplicate rows from table in OracleFinding duplicate values in a SQL tableHow to select rows with no matching entry in another table?Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraintsUpdating foreign keys while inserting into new tableForeign Key to non-primary keyRemove duplicate rows ERROR: duplicate key valueOverlapping Foreign Keys and Cascade On DeleteHow to get a count of rows (across tables) dependent on a particular column which is referenced as a foreign key in PostgresConstraints for foreign keys like this in associative table?

Does an object always see its latest internal state irrespective of thread?

Is it tax fraud for an individual to declare non-taxable revenue as taxable income? (US tax laws)

Why does Kotter return in Welcome Back Kotter?

tikz convert color string to hex value

How much of data wrangling is a data scientist's job?

Why do I get two different answers for this counting problem?

Can a vampire attack twice with their claws using Multiattack?

Is it inappropriate for a student to attend their mentor's dissertation defense?

Alternative to sending password over mail?

Can you really stack all of this on an Opportunity Attack?

Today is the Center

Rock identification in KY

Replacing matching entries in one column of a file by another column from a different file

NMaximize is not converging to a solution

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?

What's the output of a record needle playing an out-of-speed record

Watching something be written to a file live with tail

Did Shadowfax go to Valinor?

What does it mean to describe someone as a butt steak?

Are the number of citations and number of published articles the most important criteria for a tenure promotion?

Why is 150k or 200k jobs considered good when there's 300k+ births a month?

When a company launches a new product do they "come out" with a new product or do they "come up" with a new product?

How can bays and straits be determined in a procedurally generated map?

Modeling an IP Address



Remove duplicate rows in table with dependent foreign keys


How to remove duplicate rows with foreign keys dependencies?Removing duplicate rows from table in OracleFinding duplicate values in a SQL tableHow to select rows with no matching entry in another table?Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraintsUpdating foreign keys while inserting into new tableForeign Key to non-primary keyRemove duplicate rows ERROR: duplicate key valueOverlapping Foreign Keys and Cascade On DeleteHow to get a count of rows (across tables) dependent on a particular column which is referenced as a foreign key in PostgresConstraints for foreign keys like this in associative table?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Somehow a table in my PostgreSQL database had every row duplicated, so there are now two rows for every id (primary key -- see attached sample image below). Another column in that table, FAN is a foreign key in another table holdings. When I tried to delete the duplicates in table surveys with:



DELETE
FROM
surveys a
USING surveys b
WHERE
a.id = b.id;


I got the error:




ERROR: update or delete on table "surveys" violates
foreign key constraint
holdings_FAN_id_ca1342a3_fk_surveys_id" on
table "holdings"




I tried the solution to this SO question, but the the output was just an empty id column (no rows, no values), and the original surveys table was unchanged. I'm new to SQL and don't fully understand what was going on in those commands, so I may have missed something.



How can I remove the duplicate rows in my table?



Using PostgreSQL 9.6.10. Tables were created with Django models (sample code added based on comment):



class surveys(models.Model): 
FAN = models.SlugField(max_length=100, blank=True, null=True)
class holdings(models.Model):
FAN = models.ForeignKey('surveys', on_delete=models.SET_NULL, blank=True, null=True)


enter image description here










share|improve this question



















  • 1





    Duplicated values in a primary key column are basically not possible in SQL. You might want to update your question with sample data and expected output to clarify it a little.

    – GMB
    Mar 21 at 22:57











  • Instead of talking about your table definition you should provide the CREATE TABLE statement defining it. And always your version of Postgres.

    – Erwin Brandstetter
    Mar 21 at 22:58






  • 2





    A PK column with duplicate entries? I have never seen such a thing before. Looks like your database is seriously broken. Make a backup if you can ASAP ...

    – Erwin Brandstetter
    Mar 21 at 23:01






  • 1





    I wonder, if OP just assumes there are duplicates in the data. I assume instead, that the query produces duplicates (probably some join and no distinct or something.) please check the query that produces your screenshotted result set (and post it perhaps). try distinct immediately after the select keyword. as erwin notes correctly, primary keys can't have duplicates, by definition.

    – Jakumi
    Mar 22 at 0:29












  • The screenshot was just from the view data button for that table in pgAdmin III. I have reason to believe it wasn't just a display issue or an assumption because the Django site that uses it raised an error that there were two values per id. So, it seems to somehow be possible to create duplicate primary keys in postgresql.

    – Bird
    Mar 22 at 16:59

















0















Somehow a table in my PostgreSQL database had every row duplicated, so there are now two rows for every id (primary key -- see attached sample image below). Another column in that table, FAN is a foreign key in another table holdings. When I tried to delete the duplicates in table surveys with:



DELETE
FROM
surveys a
USING surveys b
WHERE
a.id = b.id;


I got the error:




ERROR: update or delete on table "surveys" violates
foreign key constraint
holdings_FAN_id_ca1342a3_fk_surveys_id" on
table "holdings"




I tried the solution to this SO question, but the the output was just an empty id column (no rows, no values), and the original surveys table was unchanged. I'm new to SQL and don't fully understand what was going on in those commands, so I may have missed something.



How can I remove the duplicate rows in my table?



Using PostgreSQL 9.6.10. Tables were created with Django models (sample code added based on comment):



class surveys(models.Model): 
FAN = models.SlugField(max_length=100, blank=True, null=True)
class holdings(models.Model):
FAN = models.ForeignKey('surveys', on_delete=models.SET_NULL, blank=True, null=True)


enter image description here










share|improve this question



















  • 1





    Duplicated values in a primary key column are basically not possible in SQL. You might want to update your question with sample data and expected output to clarify it a little.

    – GMB
    Mar 21 at 22:57











  • Instead of talking about your table definition you should provide the CREATE TABLE statement defining it. And always your version of Postgres.

    – Erwin Brandstetter
    Mar 21 at 22:58






  • 2





    A PK column with duplicate entries? I have never seen such a thing before. Looks like your database is seriously broken. Make a backup if you can ASAP ...

    – Erwin Brandstetter
    Mar 21 at 23:01






  • 1





    I wonder, if OP just assumes there are duplicates in the data. I assume instead, that the query produces duplicates (probably some join and no distinct or something.) please check the query that produces your screenshotted result set (and post it perhaps). try distinct immediately after the select keyword. as erwin notes correctly, primary keys can't have duplicates, by definition.

    – Jakumi
    Mar 22 at 0:29












  • The screenshot was just from the view data button for that table in pgAdmin III. I have reason to believe it wasn't just a display issue or an assumption because the Django site that uses it raised an error that there were two values per id. So, it seems to somehow be possible to create duplicate primary keys in postgresql.

    – Bird
    Mar 22 at 16:59













0












0








0








Somehow a table in my PostgreSQL database had every row duplicated, so there are now two rows for every id (primary key -- see attached sample image below). Another column in that table, FAN is a foreign key in another table holdings. When I tried to delete the duplicates in table surveys with:



DELETE
FROM
surveys a
USING surveys b
WHERE
a.id = b.id;


I got the error:




ERROR: update or delete on table "surveys" violates
foreign key constraint
holdings_FAN_id_ca1342a3_fk_surveys_id" on
table "holdings"




I tried the solution to this SO question, but the the output was just an empty id column (no rows, no values), and the original surveys table was unchanged. I'm new to SQL and don't fully understand what was going on in those commands, so I may have missed something.



How can I remove the duplicate rows in my table?



Using PostgreSQL 9.6.10. Tables were created with Django models (sample code added based on comment):



class surveys(models.Model): 
FAN = models.SlugField(max_length=100, blank=True, null=True)
class holdings(models.Model):
FAN = models.ForeignKey('surveys', on_delete=models.SET_NULL, blank=True, null=True)


enter image description here










share|improve this question
















Somehow a table in my PostgreSQL database had every row duplicated, so there are now two rows for every id (primary key -- see attached sample image below). Another column in that table, FAN is a foreign key in another table holdings. When I tried to delete the duplicates in table surveys with:



DELETE
FROM
surveys a
USING surveys b
WHERE
a.id = b.id;


I got the error:




ERROR: update or delete on table "surveys" violates
foreign key constraint
holdings_FAN_id_ca1342a3_fk_surveys_id" on
table "holdings"




I tried the solution to this SO question, but the the output was just an empty id column (no rows, no values), and the original surveys table was unchanged. I'm new to SQL and don't fully understand what was going on in those commands, so I may have missed something.



How can I remove the duplicate rows in my table?



Using PostgreSQL 9.6.10. Tables were created with Django models (sample code added based on comment):



class surveys(models.Model): 
FAN = models.SlugField(max_length=100, blank=True, null=True)
class holdings(models.Model):
FAN = models.ForeignKey('surveys', on_delete=models.SET_NULL, blank=True, null=True)


enter image description here







sql postgresql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 23:03







Bird

















asked Mar 21 at 22:49









BirdBird

7073934




7073934







  • 1





    Duplicated values in a primary key column are basically not possible in SQL. You might want to update your question with sample data and expected output to clarify it a little.

    – GMB
    Mar 21 at 22:57











  • Instead of talking about your table definition you should provide the CREATE TABLE statement defining it. And always your version of Postgres.

    – Erwin Brandstetter
    Mar 21 at 22:58






  • 2





    A PK column with duplicate entries? I have never seen such a thing before. Looks like your database is seriously broken. Make a backup if you can ASAP ...

    – Erwin Brandstetter
    Mar 21 at 23:01






  • 1





    I wonder, if OP just assumes there are duplicates in the data. I assume instead, that the query produces duplicates (probably some join and no distinct or something.) please check the query that produces your screenshotted result set (and post it perhaps). try distinct immediately after the select keyword. as erwin notes correctly, primary keys can't have duplicates, by definition.

    – Jakumi
    Mar 22 at 0:29












  • The screenshot was just from the view data button for that table in pgAdmin III. I have reason to believe it wasn't just a display issue or an assumption because the Django site that uses it raised an error that there were two values per id. So, it seems to somehow be possible to create duplicate primary keys in postgresql.

    – Bird
    Mar 22 at 16:59












  • 1





    Duplicated values in a primary key column are basically not possible in SQL. You might want to update your question with sample data and expected output to clarify it a little.

    – GMB
    Mar 21 at 22:57











  • Instead of talking about your table definition you should provide the CREATE TABLE statement defining it. And always your version of Postgres.

    – Erwin Brandstetter
    Mar 21 at 22:58






  • 2





    A PK column with duplicate entries? I have never seen such a thing before. Looks like your database is seriously broken. Make a backup if you can ASAP ...

    – Erwin Brandstetter
    Mar 21 at 23:01






  • 1





    I wonder, if OP just assumes there are duplicates in the data. I assume instead, that the query produces duplicates (probably some join and no distinct or something.) please check the query that produces your screenshotted result set (and post it perhaps). try distinct immediately after the select keyword. as erwin notes correctly, primary keys can't have duplicates, by definition.

    – Jakumi
    Mar 22 at 0:29












  • The screenshot was just from the view data button for that table in pgAdmin III. I have reason to believe it wasn't just a display issue or an assumption because the Django site that uses it raised an error that there were two values per id. So, it seems to somehow be possible to create duplicate primary keys in postgresql.

    – Bird
    Mar 22 at 16:59







1




1





Duplicated values in a primary key column are basically not possible in SQL. You might want to update your question with sample data and expected output to clarify it a little.

– GMB
Mar 21 at 22:57





Duplicated values in a primary key column are basically not possible in SQL. You might want to update your question with sample data and expected output to clarify it a little.

– GMB
Mar 21 at 22:57













Instead of talking about your table definition you should provide the CREATE TABLE statement defining it. And always your version of Postgres.

– Erwin Brandstetter
Mar 21 at 22:58





Instead of talking about your table definition you should provide the CREATE TABLE statement defining it. And always your version of Postgres.

– Erwin Brandstetter
Mar 21 at 22:58




2




2





A PK column with duplicate entries? I have never seen such a thing before. Looks like your database is seriously broken. Make a backup if you can ASAP ...

– Erwin Brandstetter
Mar 21 at 23:01





A PK column with duplicate entries? I have never seen such a thing before. Looks like your database is seriously broken. Make a backup if you can ASAP ...

– Erwin Brandstetter
Mar 21 at 23:01




1




1





I wonder, if OP just assumes there are duplicates in the data. I assume instead, that the query produces duplicates (probably some join and no distinct or something.) please check the query that produces your screenshotted result set (and post it perhaps). try distinct immediately after the select keyword. as erwin notes correctly, primary keys can't have duplicates, by definition.

– Jakumi
Mar 22 at 0:29






I wonder, if OP just assumes there are duplicates in the data. I assume instead, that the query produces duplicates (probably some join and no distinct or something.) please check the query that produces your screenshotted result set (and post it perhaps). try distinct immediately after the select keyword. as erwin notes correctly, primary keys can't have duplicates, by definition.

– Jakumi
Mar 22 at 0:29














The screenshot was just from the view data button for that table in pgAdmin III. I have reason to believe it wasn't just a display issue or an assumption because the Django site that uses it raised an error that there were two values per id. So, it seems to somehow be possible to create duplicate primary keys in postgresql.

– Bird
Mar 22 at 16:59





The screenshot was just from the view data button for that table in pgAdmin III. I have reason to believe it wasn't just a display issue or an assumption because the Django site that uses it raised an error that there were two values per id. So, it seems to somehow be possible to create duplicate primary keys in postgresql.

– Bird
Mar 22 at 16:59












0






active

oldest

votes












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%2f55290390%2fremove-duplicate-rows-in-table-with-dependent-foreign-keys%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















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%2f55290390%2fremove-duplicate-rows-in-table-with-dependent-foreign-keys%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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript