{"id":1,"date":"2016-10-29T19:38:25","date_gmt":"2016-10-29T19:38:25","guid":{"rendered":"http:\/\/wordpress.cs.vt.edu\/tianyi\/?p=1"},"modified":"2018-03-16T15:57:11","modified_gmt":"2018-03-16T15:57:11","slug":"hello-world","status":"publish","type":"post","link":"https:\/\/wordpress.cs.vt.edu\/tianyi\/2016\/10\/29\/hello-world\/","title":{"rendered":"Change Primary Key in PostgreSQL Tables via Django API (used as Foreign Key by other tables)"},"content":{"rendered":"<p>MTurk assignment ID is not always unique. When a worker return a HIT, the next worker taking it will have the same assignment ID.<\/p>\n<p>Such being the case, when designing data structures for our own system to deploy tasks on MTurk iframe, it&#8217;s important not to use assignment ID as primary key for any table. However, if you already did this, here&#8217;s how I fixed it in Django (using PostgreSQL):<\/p>\n<ol>\n<li>You have an old table (defined in model.py), where you specify the assignment ID column to be primary key.<br \/>\nCreate new model object with exactly the same columns, except that: the model name is different (of course!) and the primary_key=True of your assignment ID column is removed<\/p>\n<pre>Example\r\n\tclass Task(models.Model):\r\n\t# other fields\r\n\tassignment_id = models.CharField(max_length=30, primary_key = True)\r\n\tclass NewTable(models.Model):\r\n\t# other fields\r\n\tassignment_id = models.CharField(max_length=30)\r\n\r\n\tclass Result(models.Model):\r\n\t# other fields\r\n\ttask = models.ForeignKey(Task)\r\n\t# add the following line to all tables that refers your task object as foreign key\r\n\t<strong>newtable = models.ForeignKey(NewTable, null=True)<\/strong><\/pre>\n<\/li>\n<li>run in command line:\n<pre> python manage.py makemigrations\r\n\r\n\t python manage.py migrate<\/pre>\n<\/li>\n<li>Now you will have a empty new table with the correct schema constraints, the next thing to do is to copy existing contents to the new table.\n<ul>\n<li style=\"list-style-type: none\">\n<ul>\n<li>You can choose to put your scripts in [yourappname]\/management\/commans\/ folder, then run python manage.py [your filename without .py] to execute your code<\/li>\n<li>Or you can make your changes directly in command line. Following is an example<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>python manage.py shell<\/pre>\n<pre>from [yourappanme].models import *\r\n\t\tfor t in Task.objects.all():\r\n\t\t NewTable.objects.create(assignment_id=t.assignment_id, ...[other fields])\r\n\t\tfor t in Result.objects.all():\r\n\t\t nt = NewTable.objects.get(assignment_id=t.taskmeta.assignment_id)\r\n\t\t t.newtable = nt\r\n\t\t t.save()<\/pre>\n<\/li>\n<li>Now the new table is also a foreign key of those tables, you can remove the old task objects in your\u00a0<em>models.py<\/em>, and migrate the changes<\/li>\n<li>Change the model name and field names (as foreign key) of NewTable in your <em>models.py, <\/em>then migrate the changes<\/li>\n<li>Done!<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>MTurk assignment ID is not always unique. When a worker return a HIT, the next worker taking it will have the same assignment ID. Such being the case, when designing data structures for our own system to deploy tasks on MTurk iframe, it&#8217;s important not to use assignment ID as primary key for any table. &hellip; <a href=\"https:\/\/wordpress.cs.vt.edu\/tianyi\/2016\/10\/29\/hello-world\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Change Primary Key in PostgreSQL Tables via Django API (used as Foreign Key by other tables)<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":60,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,4,6,5],"tags":[],"class_list":["post-1","post","type-post","status-publish","format-standard","hentry","category-development","category-django","category-mturk","category-psql"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/wordpress.cs.vt.edu\/tianyi\/wp-json\/wp\/v2\/posts\/1","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.cs.vt.edu\/tianyi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.cs.vt.edu\/tianyi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.cs.vt.edu\/tianyi\/wp-json\/wp\/v2\/users\/60"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.cs.vt.edu\/tianyi\/wp-json\/wp\/v2\/comments?post=1"}],"version-history":[{"count":3,"href":"https:\/\/wordpress.cs.vt.edu\/tianyi\/wp-json\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":7,"href":"https:\/\/wordpress.cs.vt.edu\/tianyi\/wp-json\/wp\/v2\/posts\/1\/revisions\/7"}],"wp:attachment":[{"href":"https:\/\/wordpress.cs.vt.edu\/tianyi\/wp-json\/wp\/v2\/media?parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.cs.vt.edu\/tianyi\/wp-json\/wp\/v2\/categories?post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.cs.vt.edu\/tianyi\/wp-json\/wp\/v2\/tags?post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}