{"action":"created","comment":{"url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/pulls/comments/202388213","pull_request_review_id":137083314,"id":202388213,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIwMjM4ODIxMw==","diff_hunk":"@@ -87,6 +87,137 @@ There is built-in support for this option for Django >= 1.9. To use `sortkey`, s\n \n N.B.: there is no validation of this option, instead we let Redshift validate it for you. Be sure to refer to the `documentation <http://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_examples.html>`_.\n \n+Using distkey\n+---------------------------------\n+\n+There is built-in support for this option for Django >= 1.11. To use `distkey`, define an index on the model\n+meta with the custom index type `django_redshift_backend.base.DistKey` with `fields` naming a single field::\n+\n+  class MyModel(models.Model):\n+      ...\n+\n+      class Meta:\n+          indexes = [DistKey(fields=['customer_id'])]\n+\n+Redshift doesn't have conventional indexes, and we don't generate SQL for them. We merely use\n+`indexes` as a convenient place in the Meta to identify the `distkey`.\n+\n+You will likely encounter the following complication:\n+\n+Inlining Index Migrations\n+~~~~~~~~~~~~~~~~~~~~~~~~~\n+Django's `makemigrations` generates a migration file that first applies a `CreateModel` operation without the\n+`indexes` option, and then adds the index in a separate `AddIndex` operation.\n+\n+However Redshift requires that the `distkey` be specified at table creation. As a result, you may need to\n+manually edit your migration files to move the index creation into the initial `CreateModel`.\n+\n+That is, to go from::\n+\n+    operations = [\n+        ...\n+        migrations.CreateModel(\n+            name='FactTable',\n+            fields=[\n+                ('distkeycol', models.CharField()),\n+                ('measure1', models.IntegerField()),\n+                ('measure2', models.IntegerField())\n+                ...\n+            ]\n+        ),\n+       ...\n+       migrations.AddIndex(\n+            model_name='facttable',\n+            index=django_redshift_backend.base.DistKey(fields=['distkeycol'], name='...'),\n+        ),\n+    ]\n+\n+To::\n+\n+    operations = [\n+        ...\n+        migrations.CreateModel(\n+            name='FactTable',\n+            fields=[\n+                ('distkeycol', models.CharField()),\n+                ('measure1', models.IntegerField()),\n+                ('measure2', models.IntegerField())\n+                ...\n+            ],\n+            options={\n+                'indexes': [django_redshift_backend.base.DistKey(fields=['distkeycol'], name='...')],\n+            },\n+        ),\n+       ...\n+    ]\n+\n+\n+Inlining ForeignKey Migrations\n+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n+It is common to distribute fact tables on a foreign key column referencing the primary key of a dimension table.\n+\n+In this case you may also encounter the following added complication:\n+\n+Django's `makemigrations` generates a migration file that first applies a `CreateModel` operation without the\n+`ForeignKey` column, and then adds the `ForeignKey` column in a separate `AddField` operation.  It does this to\n+avoid attempts to create foreign key constraints against tables that haven't been created yet.\n+\n+However Redshift requires that the `distkey` be specified at table creation. As a result, you may need to\n+manually edit your migration files to move the ForeignKey column into the initial `CreateModel`, while also\n+ensuring that the referenced table appears *before* the referencing table in the file.","path":"README.rst","position":81,"original_position":81,"commit_id":"8ccb707e30f72cd144fa873dd05fc290bc0e4c93","original_commit_id":"e1f61c74b5694a4375d20bef06ac540d518298e0","user":{"login":"shimizukawa","id":151623,"node_id":"MDQ6VXNlcjE1MTYyMw==","avatar_url":"https://avatars1.githubusercontent.com/u/151623?v=4","gravatar_id":"","url":"https://api.github.com/users/shimizukawa","html_url":"https://github.com/shimizukawa","followers_url":"https://api.github.com/users/shimizukawa/followers","following_url":"https://api.github.com/users/shimizukawa/following{/other_user}","gists_url":"https://api.github.com/users/shimizukawa/gists{/gist_id}","starred_url":"https://api.github.com/users/shimizukawa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shimizukawa/subscriptions","organizations_url":"https://api.github.com/users/shimizukawa/orgs","repos_url":"https://api.github.com/users/shimizukawa/repos","events_url":"https://api.github.com/users/shimizukawa/events{/privacy}","received_events_url":"https://api.github.com/users/shimizukawa/received_events","type":"User","site_admin":false},"body":"No objection from me.\r\n","created_at":"2018-07-13T15:30:37Z","updated_at":"2018-07-13T15:33:33Z","html_url":"https://github.com/shimizukawa/django-redshift-backend/pull/42#discussion_r202388213","pull_request_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/pulls/42","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/shimizukawa/django-redshift-backend/pulls/comments/202388213"},"html":{"href":"https://github.com/shimizukawa/django-redshift-backend/pull/42#discussion_r202388213"},"pull_request":{"href":"https://api.github.com/repos/shimizukawa/django-redshift-backend/pulls/42"}},"in_reply_to_id":200845190},"pull_request":{"url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/pulls/42","id":199942896,"node_id":"MDExOlB1bGxSZXF1ZXN0MTk5OTQyODk2","html_url":"https://github.com/shimizukawa/django-redshift-backend/pull/42","diff_url":"https://github.com/shimizukawa/django-redshift-backend/pull/42.diff","patch_url":"https://github.com/shimizukawa/django-redshift-backend/pull/42.patch","issue_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/issues/42","number":42,"state":"open","locked":false,"title":"Add support for distkey.","user":{"login":"benjyw","id":512764,"node_id":"MDQ6VXNlcjUxMjc2NA==","avatar_url":"https://avatars2.githubusercontent.com/u/512764?v=4","gravatar_id":"","url":"https://api.github.com/users/benjyw","html_url":"https://github.com/benjyw","followers_url":"https://api.github.com/users/benjyw/followers","following_url":"https://api.github.com/users/benjyw/following{/other_user}","gists_url":"https://api.github.com/users/benjyw/gists{/gist_id}","starred_url":"https://api.github.com/users/benjyw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benjyw/subscriptions","organizations_url":"https://api.github.com/users/benjyw/orgs","repos_url":"https://api.github.com/users/benjyw/repos","events_url":"https://api.github.com/users/benjyw/events{/privacy}","received_events_url":"https://api.github.com/users/benjyw/received_events","type":"User","site_admin":false},"body":"Subject: Add support for setting a DISTKEY.\r\n\r\n### Feature or Bugfix\r\n- Feature\r\n\r\n### Purpose\r\nSetting a DISTKEY is a basic requirement in real-world Redshift clusters. \r\nThis change adds support for designating a column as the DISTKEY.\r\nThere are some complications and caveats, explained in the README.\r\n\r\n\r\n### Detail\r\nThis is implemented by setting a custom index type in the model's meta class:\r\n\r\n```\r\nfrom django_redshift_backend.base import DistKey\r\n...\r\nclass Fact(Model):\r\n  class Meta:\r\n    indexes = [DistKey(fields=['dim1'])]\r\n\r\n  dim1 = ForeignKey(Dimension1Model)\r\n```\r\n\r\nBut, as explained in the README, this will likely require some hand-editing\r\nof the migration files, so that all necessary information is available at \r\ntable creation time.\r\n\r\nThis change also fixes a minor issue with the SORTKEY support, where\r\nit was assumed that the db column name was the same as the field name.\r\nBut this is not the case, e.g., for ForeignKey fields.\r\n\r\nThis change also replaces references to the defunct `remote_field.to` with\r\nthe `remote_field.related_model`.\r\n\r\n### Relates\r\nhttps://github.com/shimizukawa/django-redshift-backend/issues/16\r\nhttps://github.com/shimizukawa/django-redshift-backend/pull/20\r\n\r\n","created_at":"2018-07-08T13:46:05Z","updated_at":"2018-07-13T15:33:33Z","closed_at":null,"merged_at":null,"merge_commit_sha":"296623b7a62146125265a296454f6b81df598f7e","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/pulls/42/commits","review_comments_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/pulls/42/comments","review_comment_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/pulls/comments{/number}","comments_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/issues/42/comments","statuses_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/statuses/8ccb707e30f72cd144fa873dd05fc290bc0e4c93","head":{"label":"benjyw:distkey_support","ref":"distkey_support","sha":"8ccb707e30f72cd144fa873dd05fc290bc0e4c93","user":{"login":"benjyw","id":512764,"node_id":"MDQ6VXNlcjUxMjc2NA==","avatar_url":"https://avatars2.githubusercontent.com/u/512764?v=4","gravatar_id":"","url":"https://api.github.com/users/benjyw","html_url":"https://github.com/benjyw","followers_url":"https://api.github.com/users/benjyw/followers","following_url":"https://api.github.com/users/benjyw/following{/other_user}","gists_url":"https://api.github.com/users/benjyw/gists{/gist_id}","starred_url":"https://api.github.com/users/benjyw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benjyw/subscriptions","organizations_url":"https://api.github.com/users/benjyw/orgs","repos_url":"https://api.github.com/users/benjyw/repos","events_url":"https://api.github.com/users/benjyw/events{/privacy}","received_events_url":"https://api.github.com/users/benjyw/received_events","type":"User","site_admin":false},"repo":{"id":137531554,"node_id":"MDEwOlJlcG9zaXRvcnkxMzc1MzE1NTQ=","name":"django-redshift-backend","full_name":"benjyw/django-redshift-backend","owner":{"login":"benjyw","id":512764,"node_id":"MDQ6VXNlcjUxMjc2NA==","avatar_url":"https://avatars2.githubusercontent.com/u/512764?v=4","gravatar_id":"","url":"https://api.github.com/users/benjyw","html_url":"https://github.com/benjyw","followers_url":"https://api.github.com/users/benjyw/followers","following_url":"https://api.github.com/users/benjyw/following{/other_user}","gists_url":"https://api.github.com/users/benjyw/gists{/gist_id}","starred_url":"https://api.github.com/users/benjyw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benjyw/subscriptions","organizations_url":"https://api.github.com/users/benjyw/orgs","repos_url":"https://api.github.com/users/benjyw/repos","events_url":"https://api.github.com/users/benjyw/events{/privacy}","received_events_url":"https://api.github.com/users/benjyw/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/benjyw/django-redshift-backend","description":"Redshift database backend for Django","fork":true,"url":"https://api.github.com/repos/benjyw/django-redshift-backend","forks_url":"https://api.github.com/repos/benjyw/django-redshift-backend/forks","keys_url":"https://api.github.com/repos/benjyw/django-redshift-backend/keys{/key_id}","collaborators_url":"https://api.github.com/repos/benjyw/django-redshift-backend/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/benjyw/django-redshift-backend/teams","hooks_url":"https://api.github.com/repos/benjyw/django-redshift-backend/hooks","issue_events_url":"https://api.github.com/repos/benjyw/django-redshift-backend/issues/events{/number}","events_url":"https://api.github.com/repos/benjyw/django-redshift-backend/events","assignees_url":"https://api.github.com/repos/benjyw/django-redshift-backend/assignees{/user}","branches_url":"https://api.github.com/repos/benjyw/django-redshift-backend/branches{/branch}","tags_url":"https://api.github.com/repos/benjyw/django-redshift-backend/tags","blobs_url":"https://api.github.com/repos/benjyw/django-redshift-backend/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/benjyw/django-redshift-backend/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/benjyw/django-redshift-backend/git/refs{/sha}","trees_url":"https://api.github.com/repos/benjyw/django-redshift-backend/git/trees{/sha}","statuses_url":"https://api.github.com/repos/benjyw/django-redshift-backend/statuses/{sha}","languages_url":"https://api.github.com/repos/benjyw/django-redshift-backend/languages","stargazers_url":"https://api.github.com/repos/benjyw/django-redshift-backend/stargazers","contributors_url":"https://api.github.com/repos/benjyw/django-redshift-backend/contributors","subscribers_url":"https://api.github.com/repos/benjyw/django-redshift-backend/subscribers","subscription_url":"https://api.github.com/repos/benjyw/django-redshift-backend/subscription","commits_url":"https://api.github.com/repos/benjyw/django-redshift-backend/commits{/sha}","git_commits_url":"https://api.github.com/repos/benjyw/django-redshift-backend/git/commits{/sha}","comments_url":"https://api.github.com/repos/benjyw/django-redshift-backend/comments{/number}","issue_comment_url":"https://api.github.com/repos/benjyw/django-redshift-backend/issues/comments{/number}","contents_url":"https://api.github.com/repos/benjyw/django-redshift-backend/contents/{+path}","compare_url":"https://api.github.com/repos/benjyw/django-redshift-backend/compare/{base}...{head}","merges_url":"https://api.github.com/repos/benjyw/django-redshift-backend/merges","archive_url":"https://api.github.com/repos/benjyw/django-redshift-backend/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/benjyw/django-redshift-backend/downloads","issues_url":"https://api.github.com/repos/benjyw/django-redshift-backend/issues{/number}","pulls_url":"https://api.github.com/repos/benjyw/django-redshift-backend/pulls{/number}","milestones_url":"https://api.github.com/repos/benjyw/django-redshift-backend/milestones{/number}","notifications_url":"https://api.github.com/repos/benjyw/django-redshift-backend/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/benjyw/django-redshift-backend/labels{/name}","releases_url":"https://api.github.com/repos/benjyw/django-redshift-backend/releases{/id}","deployments_url":"https://api.github.com/repos/benjyw/django-redshift-backend/deployments","created_at":"2018-06-15T21:01:08Z","updated_at":"2018-07-07T08:37:02Z","pushed_at":"2018-07-09T21:53:04Z","git_url":"git://github.com/benjyw/django-redshift-backend.git","ssh_url":"git@github.com:benjyw/django-redshift-backend.git","clone_url":"https://github.com/benjyw/django-redshift-backend.git","svn_url":"https://github.com/benjyw/django-redshift-backend","homepage":null,"size":2440,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"shimizukawa:master","ref":"master","sha":"447250d6252dcd30b41a6007b94d0e8f5b492963","user":{"login":"shimizukawa","id":151623,"node_id":"MDQ6VXNlcjE1MTYyMw==","avatar_url":"https://avatars1.githubusercontent.com/u/151623?v=4","gravatar_id":"","url":"https://api.github.com/users/shimizukawa","html_url":"https://github.com/shimizukawa","followers_url":"https://api.github.com/users/shimizukawa/followers","following_url":"https://api.github.com/users/shimizukawa/following{/other_user}","gists_url":"https://api.github.com/users/shimizukawa/gists{/gist_id}","starred_url":"https://api.github.com/users/shimizukawa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shimizukawa/subscriptions","organizations_url":"https://api.github.com/users/shimizukawa/orgs","repos_url":"https://api.github.com/users/shimizukawa/repos","events_url":"https://api.github.com/users/shimizukawa/events{/privacy}","received_events_url":"https://api.github.com/users/shimizukawa/received_events","type":"User","site_admin":false},"repo":{"id":32789222,"node_id":"MDEwOlJlcG9zaXRvcnkzMjc4OTIyMg==","name":"django-redshift-backend","full_name":"shimizukawa/django-redshift-backend","owner":{"login":"shimizukawa","id":151623,"node_id":"MDQ6VXNlcjE1MTYyMw==","avatar_url":"https://avatars1.githubusercontent.com/u/151623?v=4","gravatar_id":"","url":"https://api.github.com/users/shimizukawa","html_url":"https://github.com/shimizukawa","followers_url":"https://api.github.com/users/shimizukawa/followers","following_url":"https://api.github.com/users/shimizukawa/following{/other_user}","gists_url":"https://api.github.com/users/shimizukawa/gists{/gist_id}","starred_url":"https://api.github.com/users/shimizukawa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shimizukawa/subscriptions","organizations_url":"https://api.github.com/users/shimizukawa/orgs","repos_url":"https://api.github.com/users/shimizukawa/repos","events_url":"https://api.github.com/users/shimizukawa/events{/privacy}","received_events_url":"https://api.github.com/users/shimizukawa/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/shimizukawa/django-redshift-backend","description":"Redshift database backend for Django","fork":false,"url":"https://api.github.com/repos/shimizukawa/django-redshift-backend","forks_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/forks","keys_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/keys{/key_id}","collaborators_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/teams","hooks_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/hooks","issue_events_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/issues/events{/number}","events_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/events","assignees_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/assignees{/user}","branches_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/branches{/branch}","tags_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/tags","blobs_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/git/refs{/sha}","trees_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/git/trees{/sha}","statuses_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/statuses/{sha}","languages_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/languages","stargazers_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/stargazers","contributors_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/contributors","subscribers_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/subscribers","subscription_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/subscription","commits_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/commits{/sha}","git_commits_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/git/commits{/sha}","comments_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/comments{/number}","issue_comment_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/issues/comments{/number}","contents_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/contents/{+path}","compare_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/compare/{base}...{head}","merges_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/merges","archive_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/downloads","issues_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/issues{/number}","pulls_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/pulls{/number}","milestones_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/milestones{/number}","notifications_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/labels{/name}","releases_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/releases{/id}","deployments_url":"https://api.github.com/repos/shimizukawa/django-redshift-backend/deployments","created_at":"2015-03-24T09:35:35Z","updated_at":"2018-07-10T14:56:10Z","pushed_at":"2018-07-10T14:56:08Z","git_url":"git://github.com/shimizukawa/django-redshift-backend.git","ssh_url":"git@github.com:shimizukawa/django-redshift-backend.git","clone_url":"https://github.com/shimizukawa/django-redshift-backend.git","svn_url":"https://github.com/shimizukawa/django-redshift-backend","homepage":null,"size":2437,"stargazers_count":42,"watchers_count":42,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":18,"mirror_url":null,"archived":false,"open_issues_count":9,"license":null,"forks":18,"open_issues":9,"watchers":42,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/shimizukawa/django-redshift-backend/pulls/42"},"html":{"href":"https://github.com/shimizukawa/django-redshift-backend/pull/42"},"issue":{"href":"https://api.github.com/repos/shimizukawa/django-redshift-backend/issues/42"},"comments":{"href":"https://api.github.com/repos/shimizukawa/django-redshift-backend/issues/42/comments"},"review_comments":{"href":"https://api.github.com/repos/shimizukawa/django-redshift-backend/pulls/42/comments"},"review_comment":{"href":"https://api.github.com/repos/shimizukawa/django-redshift-backend/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/shimizukawa/django-redshift-backend/pulls/42/commits"},"statuses":{"href":"https://api.github.com/repos/shimizukawa/django-redshift-backend/statuses/8ccb707e30f72cd144fa873dd05fc290bc0e4c93"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"shimizukawa"}}