{"action":"created","comment":{"url":"https://api.github.com/repos/phoenixframework/phoenix_guides/pulls/comments/38552342","id":38552342,"diff_hunk":"@@ -0,0 +1,97 @@\n+###Example: Create a model with custom primary key `name:string` instead of autogenerated `id:integer`\n+\n+IMPORTANT NOTE: this example was chosen for simplicity.  It is not advisable to use something that is likely to change\n+in the future as primary key.  \n+\n+Create project named chat:\n+\n+```console\n+$mix phoenix.new chat\n+$cd chat\n+$mix ecto.create\n+```\n+\n+To create User model run: \n+```console\n+$mix phoenix.gen.json User users name:string age:integer\n+```\n+\n+Modify 4 generated files:\n+\n+**1. Modify models/user.ex like so:**\n+\n+* Add `@primary_key {:name, :string, []}` right above `schema \"users\" do` and remove `field :name` from below it:\n+```elixir\n+@primary_key {:name, :string, []}  # ADDED THIS LINE\n+schema \"users\" do\n+  #field :name, :string  REMOVED THIS LINE\n+  field :age, :integer\n+\n+  timestamps\n+end\n+```\n+    \n+* Add below code somewhere in this file to be able to specify name instead of id in the URL.\n+    Based on this example https://github.com/gjaldon/phoenix_params\n+```elixir\n+defimpl Phoenix.Param, for: Chat.User do \n+    def to_param(user) do\n+        user.name\n+   end\n+end\n+```\n+And below is another modification we need to be able to specify name instead of id in the URL\n+(again, based on https://github.com/gjaldon/phoenix_params)\n+\n+**2. Modify web/controllers/user_controller.ex show, update, and delete methods**\n+\n+NOTE the use of `Repo.get_by` instead of `Repo.get` and `name: name` instead of `id`","path":"HH_custom_primary_key.md","position":48,"original_position":48,"commit_id":"64b4db97727e26cc827d4f6cdc07076a077fb989","original_commit_id":"64b4db97727e26cc827d4f6cdc07076a077fb989","user":{"login":"iguberman","id":12000934,"avatar_url":"https://avatars.githubusercontent.com/u/12000934?v=3","gravatar_id":"","url":"https://api.github.com/users/iguberman","html_url":"https://github.com/iguberman","followers_url":"https://api.github.com/users/iguberman/followers","following_url":"https://api.github.com/users/iguberman/following{/other_user}","gists_url":"https://api.github.com/users/iguberman/gists{/gist_id}","starred_url":"https://api.github.com/users/iguberman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iguberman/subscriptions","organizations_url":"https://api.github.com/users/iguberman/orgs","repos_url":"https://api.github.com/users/iguberman/repos","events_url":"https://api.github.com/users/iguberman/events{/privacy}","received_events_url":"https://api.github.com/users/iguberman/received_events","type":"User","site_admin":false},"body":"Perfect -- this makes necessary changes really trivial! ","created_at":"2015-09-02T16:18:06Z","updated_at":"2015-09-02T16:18:06Z","html_url":"https://github.com/phoenixframework/phoenix_guides/pull/365#discussion_r38552342","pull_request_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/pulls/365","_links":{"self":{"href":"https://api.github.com/repos/phoenixframework/phoenix_guides/pulls/comments/38552342"},"html":{"href":"https://github.com/phoenixframework/phoenix_guides/pull/365#discussion_r38552342"},"pull_request":{"href":"https://api.github.com/repos/phoenixframework/phoenix_guides/pulls/365"}}},"pull_request":{"url":"https://api.github.com/repos/phoenixframework/phoenix_guides/pulls/365","id":43946423,"html_url":"https://github.com/phoenixframework/phoenix_guides/pull/365","diff_url":"https://github.com/phoenixframework/phoenix_guides/pull/365.diff","patch_url":"https://github.com/phoenixframework/phoenix_guides/pull/365.patch","issue_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/issues/365","number":365,"state":"open","locked":false,"title":"Create A_custom_primary_key.md","user":{"login":"iguberman","id":12000934,"avatar_url":"https://avatars.githubusercontent.com/u/12000934?v=3","gravatar_id":"","url":"https://api.github.com/users/iguberman","html_url":"https://github.com/iguberman","followers_url":"https://api.github.com/users/iguberman/followers","following_url":"https://api.github.com/users/iguberman/following{/other_user}","gists_url":"https://api.github.com/users/iguberman/gists{/gist_id}","starred_url":"https://api.github.com/users/iguberman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iguberman/subscriptions","organizations_url":"https://api.github.com/users/iguberman/orgs","repos_url":"https://api.github.com/users/iguberman/repos","events_url":"https://api.github.com/users/iguberman/events{/privacy}","received_events_url":"https://api.github.com/users/iguberman/received_events","type":"User","site_admin":false},"body":"My motivation for this document: it is important to be able to create a primary key other than id:integer in a variety of situations, especially if Phoenix is being adopted in existing projects or organizations.  I wanted to have all the steps in one place and easily accessible to Phoenix users -- especially new Phoenix users like myself.   Big thanks to Jose Valim and Slack users  @sg, @angeloashmore, @gjaldon for their help and feedback!","created_at":"2015-09-02T01:48:58Z","updated_at":"2015-09-02T16:18:06Z","closed_at":null,"merged_at":null,"merge_commit_sha":"64180c2d97f3f9b9e995f78c2fe58d8114352c9a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/pulls/365/commits","review_comments_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/pulls/365/comments","review_comment_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/pulls/comments{/number}","comments_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/issues/365/comments","statuses_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/statuses/64b4db97727e26cc827d4f6cdc07076a077fb989","head":{"label":"iguberman:iguberman-custom-primary-key-example","ref":"iguberman-custom-primary-key-example","sha":"64b4db97727e26cc827d4f6cdc07076a077fb989","user":{"login":"iguberman","id":12000934,"avatar_url":"https://avatars.githubusercontent.com/u/12000934?v=3","gravatar_id":"","url":"https://api.github.com/users/iguberman","html_url":"https://github.com/iguberman","followers_url":"https://api.github.com/users/iguberman/followers","following_url":"https://api.github.com/users/iguberman/following{/other_user}","gists_url":"https://api.github.com/users/iguberman/gists{/gist_id}","starred_url":"https://api.github.com/users/iguberman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iguberman/subscriptions","organizations_url":"https://api.github.com/users/iguberman/orgs","repos_url":"https://api.github.com/users/iguberman/repos","events_url":"https://api.github.com/users/iguberman/events{/privacy}","received_events_url":"https://api.github.com/users/iguberman/received_events","type":"User","site_admin":false},"repo":{"id":41768923,"name":"phoenix_guides","full_name":"iguberman/phoenix_guides","owner":{"login":"iguberman","id":12000934,"avatar_url":"https://avatars.githubusercontent.com/u/12000934?v=3","gravatar_id":"","url":"https://api.github.com/users/iguberman","html_url":"https://github.com/iguberman","followers_url":"https://api.github.com/users/iguberman/followers","following_url":"https://api.github.com/users/iguberman/following{/other_user}","gists_url":"https://api.github.com/users/iguberman/gists{/gist_id}","starred_url":"https://api.github.com/users/iguberman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iguberman/subscriptions","organizations_url":"https://api.github.com/users/iguberman/orgs","repos_url":"https://api.github.com/users/iguberman/repos","events_url":"https://api.github.com/users/iguberman/events{/privacy}","received_events_url":"https://api.github.com/users/iguberman/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/iguberman/phoenix_guides","description":"User guides for the Phoenix web development framework.","fork":true,"url":"https://api.github.com/repos/iguberman/phoenix_guides","forks_url":"https://api.github.com/repos/iguberman/phoenix_guides/forks","keys_url":"https://api.github.com/repos/iguberman/phoenix_guides/keys{/key_id}","collaborators_url":"https://api.github.com/repos/iguberman/phoenix_guides/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/iguberman/phoenix_guides/teams","hooks_url":"https://api.github.com/repos/iguberman/phoenix_guides/hooks","issue_events_url":"https://api.github.com/repos/iguberman/phoenix_guides/issues/events{/number}","events_url":"https://api.github.com/repos/iguberman/phoenix_guides/events","assignees_url":"https://api.github.com/repos/iguberman/phoenix_guides/assignees{/user}","branches_url":"https://api.github.com/repos/iguberman/phoenix_guides/branches{/branch}","tags_url":"https://api.github.com/repos/iguberman/phoenix_guides/tags","blobs_url":"https://api.github.com/repos/iguberman/phoenix_guides/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/iguberman/phoenix_guides/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/iguberman/phoenix_guides/git/refs{/sha}","trees_url":"https://api.github.com/repos/iguberman/phoenix_guides/git/trees{/sha}","statuses_url":"https://api.github.com/repos/iguberman/phoenix_guides/statuses/{sha}","languages_url":"https://api.github.com/repos/iguberman/phoenix_guides/languages","stargazers_url":"https://api.github.com/repos/iguberman/phoenix_guides/stargazers","contributors_url":"https://api.github.com/repos/iguberman/phoenix_guides/contributors","subscribers_url":"https://api.github.com/repos/iguberman/phoenix_guides/subscribers","subscription_url":"https://api.github.com/repos/iguberman/phoenix_guides/subscription","commits_url":"https://api.github.com/repos/iguberman/phoenix_guides/commits{/sha}","git_commits_url":"https://api.github.com/repos/iguberman/phoenix_guides/git/commits{/sha}","comments_url":"https://api.github.com/repos/iguberman/phoenix_guides/comments{/number}","issue_comment_url":"https://api.github.com/repos/iguberman/phoenix_guides/issues/comments{/number}","contents_url":"https://api.github.com/repos/iguberman/phoenix_guides/contents/{+path}","compare_url":"https://api.github.com/repos/iguberman/phoenix_guides/compare/{base}...{head}","merges_url":"https://api.github.com/repos/iguberman/phoenix_guides/merges","archive_url":"https://api.github.com/repos/iguberman/phoenix_guides/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/iguberman/phoenix_guides/downloads","issues_url":"https://api.github.com/repos/iguberman/phoenix_guides/issues{/number}","pulls_url":"https://api.github.com/repos/iguberman/phoenix_guides/pulls{/number}","milestones_url":"https://api.github.com/repos/iguberman/phoenix_guides/milestones{/number}","notifications_url":"https://api.github.com/repos/iguberman/phoenix_guides/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/iguberman/phoenix_guides/labels{/name}","releases_url":"https://api.github.com/repos/iguberman/phoenix_guides/releases{/id}","created_at":"2015-09-01T23:37:44Z","updated_at":"2015-09-01T23:37:44Z","pushed_at":"2015-09-02T04:02:25Z","git_url":"git://github.com/iguberman/phoenix_guides.git","ssh_url":"git@github.com:iguberman/phoenix_guides.git","clone_url":"https://github.com/iguberman/phoenix_guides.git","svn_url":"https://github.com/iguberman/phoenix_guides","homepage":null,"size":3485,"stargazers_count":0,"watchers_count":0,"language":"CSS","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"phoenixframework:master","ref":"master","sha":"06ec715a72df4eaf89082c5ae54684203740f1fc","user":{"login":"phoenixframework","id":6510388,"avatar_url":"https://avatars.githubusercontent.com/u/6510388?v=3","gravatar_id":"","url":"https://api.github.com/users/phoenixframework","html_url":"https://github.com/phoenixframework","followers_url":"https://api.github.com/users/phoenixframework/followers","following_url":"https://api.github.com/users/phoenixframework/following{/other_user}","gists_url":"https://api.github.com/users/phoenixframework/gists{/gist_id}","starred_url":"https://api.github.com/users/phoenixframework/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/phoenixframework/subscriptions","organizations_url":"https://api.github.com/users/phoenixframework/orgs","repos_url":"https://api.github.com/users/phoenixframework/repos","events_url":"https://api.github.com/users/phoenixframework/events{/privacy}","received_events_url":"https://api.github.com/users/phoenixframework/received_events","type":"Organization","site_admin":false},"repo":{"id":22436876,"name":"phoenix_guides","full_name":"phoenixframework/phoenix_guides","owner":{"login":"phoenixframework","id":6510388,"avatar_url":"https://avatars.githubusercontent.com/u/6510388?v=3","gravatar_id":"","url":"https://api.github.com/users/phoenixframework","html_url":"https://github.com/phoenixframework","followers_url":"https://api.github.com/users/phoenixframework/followers","following_url":"https://api.github.com/users/phoenixframework/following{/other_user}","gists_url":"https://api.github.com/users/phoenixframework/gists{/gist_id}","starred_url":"https://api.github.com/users/phoenixframework/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/phoenixframework/subscriptions","organizations_url":"https://api.github.com/users/phoenixframework/orgs","repos_url":"https://api.github.com/users/phoenixframework/repos","events_url":"https://api.github.com/users/phoenixframework/events{/privacy}","received_events_url":"https://api.github.com/users/phoenixframework/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/phoenixframework/phoenix_guides","description":"User guides for the Phoenix web development framework.","fork":false,"url":"https://api.github.com/repos/phoenixframework/phoenix_guides","forks_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/forks","keys_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/keys{/key_id}","collaborators_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/teams","hooks_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/hooks","issue_events_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/issues/events{/number}","events_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/events","assignees_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/assignees{/user}","branches_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/branches{/branch}","tags_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/tags","blobs_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/git/refs{/sha}","trees_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/git/trees{/sha}","statuses_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/statuses/{sha}","languages_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/languages","stargazers_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/stargazers","contributors_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/contributors","subscribers_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/subscribers","subscription_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/subscription","commits_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/commits{/sha}","git_commits_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/git/commits{/sha}","comments_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/comments{/number}","issue_comment_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/issues/comments{/number}","contents_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/contents/{+path}","compare_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/compare/{base}...{head}","merges_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/merges","archive_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/downloads","issues_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/issues{/number}","pulls_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/pulls{/number}","milestones_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/milestones{/number}","notifications_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/labels{/name}","releases_url":"https://api.github.com/repos/phoenixframework/phoenix_guides/releases{/id}","created_at":"2014-07-30T17:00:36Z","updated_at":"2015-09-02T12:13:05Z","pushed_at":"2015-09-02T04:02:25Z","git_url":"git://github.com/phoenixframework/phoenix_guides.git","ssh_url":"git@github.com:phoenixframework/phoenix_guides.git","clone_url":"https://github.com/phoenixframework/phoenix_guides.git","svn_url":"https://github.com/phoenixframework/phoenix_guides","homepage":null,"size":3485,"stargazers_count":228,"watchers_count":228,"language":"CSS","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":85,"mirror_url":null,"open_issues_count":16,"forks":85,"open_issues":16,"watchers":228,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/phoenixframework/phoenix_guides/pulls/365"},"html":{"href":"https://github.com/phoenixframework/phoenix_guides/pull/365"},"issue":{"href":"https://api.github.com/repos/phoenixframework/phoenix_guides/issues/365"},"comments":{"href":"https://api.github.com/repos/phoenixframework/phoenix_guides/issues/365/comments"},"review_comments":{"href":"https://api.github.com/repos/phoenixframework/phoenix_guides/pulls/365/comments"},"review_comment":{"href":"https://api.github.com/repos/phoenixframework/phoenix_guides/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/phoenixframework/phoenix_guides/pulls/365/commits"},"statuses":{"href":"https://api.github.com/repos/phoenixframework/phoenix_guides/statuses/64b4db97727e26cc827d4f6cdc07076a077fb989"}}}}