{"action":"created","comment":{"url":"https://api.github.com/repos/elixir-lang/ecto/pulls/comments/49197855","id":49197855,"diff_hunk":"@@ -238,6 +241,18 @@ defmodule Ecto.Migration.Runner do\n   defp command({:rename, %Table{} = table, current_column, new_column}),\n     do: \"rename column #{current_column} to #{new_column} on table #{quote_table(table.prefix, table.name)}\"\n \n+  defp command({:create, %Constraint{check: nil, exclude: nil}}),\n+    do: raise ArgumentError, \"a constraint must have either a check or exclude option\"\n+  defp command({:create, %Constraint{check: check, exclude: exclude}}) when is_binary(check) and is_binary(exclude),\n+    do: raise ArgumentError, \"a constraint must not have both check and exclude options\"\n+  defp command({:create, %Constraint{check: check, exclude: nil} = constraint}) when is_binary(check),","path":"lib/ecto/migration/runner.ex","position":25,"original_position":25,"commit_id":"464165a5dd7811324cf39641ef79cbfca245f8f4","original_commit_id":"464165a5dd7811324cf39641ef79cbfca245f8f4","user":{"login":"josevalim","id":9582,"avatar_url":"https://avatars.githubusercontent.com/u/9582?v=3","gravatar_id":"","url":"https://api.github.com/users/josevalim","html_url":"https://github.com/josevalim","followers_url":"https://api.github.com/users/josevalim/followers","following_url":"https://api.github.com/users/josevalim/following{/other_user}","gists_url":"https://api.github.com/users/josevalim/gists{/gist_id}","starred_url":"https://api.github.com/users/josevalim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/josevalim/subscriptions","organizations_url":"https://api.github.com/users/josevalim/orgs","repos_url":"https://api.github.com/users/josevalim/repos","events_url":"https://api.github.com/users/josevalim/events{/privacy}","received_events_url":"https://api.github.com/users/josevalim/received_events","type":"User","site_admin":false},"body":"You don't need the `exclude: nil`, nor `check: nil` below.","created_at":"2016-01-08T15:19:18Z","updated_at":"2016-01-08T15:19:18Z","html_url":"https://github.com/elixir-lang/ecto/pull/1180#discussion_r49197855","pull_request_url":"https://api.github.com/repos/elixir-lang/ecto/pulls/1180","_links":{"self":{"href":"https://api.github.com/repos/elixir-lang/ecto/pulls/comments/49197855"},"html":{"href":"https://github.com/elixir-lang/ecto/pull/1180#discussion_r49197855"},"pull_request":{"href":"https://api.github.com/repos/elixir-lang/ecto/pulls/1180"}}},"pull_request":{"url":"https://api.github.com/repos/elixir-lang/ecto/pulls/1180","id":54870941,"html_url":"https://github.com/elixir-lang/ecto/pull/1180","diff_url":"https://github.com/elixir-lang/ecto/pull/1180.diff","patch_url":"https://github.com/elixir-lang/ecto/pull/1180.patch","issue_url":"https://api.github.com/repos/elixir-lang/ecto/issues/1180","number":1180,"state":"open","locked":false,"title":"Support for check constraints (in progress)","user":{"login":"nathanl","id":338814,"avatar_url":"https://avatars.githubusercontent.com/u/338814?v=3","gravatar_id":"","url":"https://api.github.com/users/nathanl","html_url":"https://github.com/nathanl","followers_url":"https://api.github.com/users/nathanl/followers","following_url":"https://api.github.com/users/nathanl/following{/other_user}","gists_url":"https://api.github.com/users/nathanl/gists{/gist_id}","starred_url":"https://api.github.com/users/nathanl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nathanl/subscriptions","organizations_url":"https://api.github.com/users/nathanl/orgs","repos_url":"https://api.github.com/users/nathanl/repos","events_url":"https://api.github.com/users/nathanl/events{/privacy}","received_events_url":"https://api.github.com/users/nathanl/received_events","type":"User","site_admin":false},"body":"Postgres supports [\"check constraints\"](http://www.postgresql.org/docs/9.5/static/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS), which validate a row to say that,\r\nfor example, `price` must be greater than `0`, or `end_time` must be\r\ngreater than `start_time`. This PR is meant to add support for such\r\nchecks.\r\n\r\nI did **not** ask about this on the mailing list, so if you don't want this\r\nfeature, that's OK. :blush: I'm new to Elixir and Phoenix and wanted\r\nto see if I could figure this out. So I'm asking for feedback before I\r\ngo further.\r\n\r\nFWIW, of all the constraints [Postgres supports](http://www.postgresql.org/docs/9.5/static/ddl-constraints.html), check constraints are\r\nthe only ones Ecto doesn't yet support, so I thought this might be a useful\r\naddition.\r\n\r\nWhat works so far:\r\n\r\n- Adding a constraint when adding a column in a migration\r\n- Capturing the error returned by Postgres if a constraint is violated\r\n- Turning that into a readable error on the changeset\r\n\r\nWhat's still missing:\r\n\r\n- Ability to `modify` an existing column to add a constraint\r\n- Ability to remove constraints\r\n- Ability to add table constraints (not tied to a single column; \r\neg, `end_time` must be greater than `start_time`)\r\n\r\nI'm currently tinkering with this in my `Rumbl` app from \"Programming Phoenix\".\r\nI added an `other_description` column to `videos` and gave it a\r\nconstraint, like this:\r\n\r\n```elixir\r\ndef change do\r\n  alter table(:videos) do\r\n    add :other_description, :string,\r\n      check: check(\"char_length(other_description) < 5\",\r\n      name: \"other_description_must_be_short\")\r\n  end\r\nend\r\n```\r\n\r\nThen I added the constraint to `Video` like this:\r\n\r\n```elixir\r\ndef changeset(model, params \\\\ :empty) do\r\n   model\r\n   |> cast(params, @required_fields, @optional_fields)\r\n   |> foreign_key_constraint(:category_id)\r\n   |> check_constraint(:other_description, name: :other_description_must_be_short)\r\nend\r\n```\r\n\r\nWith that much, adding a long description gives the user this error:\r\n\r\n![constraint_error_no_message_given](https://cloud.githubusercontent.com/assets/338814/12066255/27ab5d58-afb3-11e5-879b-8d635c5ff8e7.png)\r\n\r\nIf I instead use\r\n\r\n```elixir\r\n|> check_constraint(:other_description,\r\n  name: :other_description_must_be_short,\r\n  message:  \"must be less than 5 characters\"\r\n)\r\n```\r\n\r\nThe user sees:\r\n\r\n![constraint_error_with_message](https://cloud.githubusercontent.com/assets/338814/12066269/544e79ee-afb3-11e5-9807-e9ed777fb337.png)\r\n\r\nShould I keep working on this? If so, what feedback do you have so far?","created_at":"2015-12-31T16:43:24Z","updated_at":"2016-01-08T15:19:18Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8458fae4ccebf563cb37c688ea11f1b587994b55","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/elixir-lang/ecto/pulls/1180/commits","review_comments_url":"https://api.github.com/repos/elixir-lang/ecto/pulls/1180/comments","review_comment_url":"https://api.github.com/repos/elixir-lang/ecto/pulls/comments{/number}","comments_url":"https://api.github.com/repos/elixir-lang/ecto/issues/1180/comments","statuses_url":"https://api.github.com/repos/elixir-lang/ecto/statuses/464165a5dd7811324cf39641ef79cbfca245f8f4","head":{"label":"nathanl:support_check_constraints","ref":"support_check_constraints","sha":"464165a5dd7811324cf39641ef79cbfca245f8f4","user":{"login":"nathanl","id":338814,"avatar_url":"https://avatars.githubusercontent.com/u/338814?v=3","gravatar_id":"","url":"https://api.github.com/users/nathanl","html_url":"https://github.com/nathanl","followers_url":"https://api.github.com/users/nathanl/followers","following_url":"https://api.github.com/users/nathanl/following{/other_user}","gists_url":"https://api.github.com/users/nathanl/gists{/gist_id}","starred_url":"https://api.github.com/users/nathanl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nathanl/subscriptions","organizations_url":"https://api.github.com/users/nathanl/orgs","repos_url":"https://api.github.com/users/nathanl/repos","events_url":"https://api.github.com/users/nathanl/events{/privacy}","received_events_url":"https://api.github.com/users/nathanl/received_events","type":"User","site_admin":false},"repo":{"id":48850260,"name":"ecto","full_name":"nathanl/ecto","owner":{"login":"nathanl","id":338814,"avatar_url":"https://avatars.githubusercontent.com/u/338814?v=3","gravatar_id":"","url":"https://api.github.com/users/nathanl","html_url":"https://github.com/nathanl","followers_url":"https://api.github.com/users/nathanl/followers","following_url":"https://api.github.com/users/nathanl/following{/other_user}","gists_url":"https://api.github.com/users/nathanl/gists{/gist_id}","starred_url":"https://api.github.com/users/nathanl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nathanl/subscriptions","organizations_url":"https://api.github.com/users/nathanl/orgs","repos_url":"https://api.github.com/users/nathanl/repos","events_url":"https://api.github.com/users/nathanl/events{/privacy}","received_events_url":"https://api.github.com/users/nathanl/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/nathanl/ecto","description":"A database wrapper and language integrated query for Elixir","fork":true,"url":"https://api.github.com/repos/nathanl/ecto","forks_url":"https://api.github.com/repos/nathanl/ecto/forks","keys_url":"https://api.github.com/repos/nathanl/ecto/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nathanl/ecto/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nathanl/ecto/teams","hooks_url":"https://api.github.com/repos/nathanl/ecto/hooks","issue_events_url":"https://api.github.com/repos/nathanl/ecto/issues/events{/number}","events_url":"https://api.github.com/repos/nathanl/ecto/events","assignees_url":"https://api.github.com/repos/nathanl/ecto/assignees{/user}","branches_url":"https://api.github.com/repos/nathanl/ecto/branches{/branch}","tags_url":"https://api.github.com/repos/nathanl/ecto/tags","blobs_url":"https://api.github.com/repos/nathanl/ecto/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nathanl/ecto/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nathanl/ecto/git/refs{/sha}","trees_url":"https://api.github.com/repos/nathanl/ecto/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nathanl/ecto/statuses/{sha}","languages_url":"https://api.github.com/repos/nathanl/ecto/languages","stargazers_url":"https://api.github.com/repos/nathanl/ecto/stargazers","contributors_url":"https://api.github.com/repos/nathanl/ecto/contributors","subscribers_url":"https://api.github.com/repos/nathanl/ecto/subscribers","subscription_url":"https://api.github.com/repos/nathanl/ecto/subscription","commits_url":"https://api.github.com/repos/nathanl/ecto/commits{/sha}","git_commits_url":"https://api.github.com/repos/nathanl/ecto/git/commits{/sha}","comments_url":"https://api.github.com/repos/nathanl/ecto/comments{/number}","issue_comment_url":"https://api.github.com/repos/nathanl/ecto/issues/comments{/number}","contents_url":"https://api.github.com/repos/nathanl/ecto/contents/{+path}","compare_url":"https://api.github.com/repos/nathanl/ecto/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nathanl/ecto/merges","archive_url":"https://api.github.com/repos/nathanl/ecto/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nathanl/ecto/downloads","issues_url":"https://api.github.com/repos/nathanl/ecto/issues{/number}","pulls_url":"https://api.github.com/repos/nathanl/ecto/pulls{/number}","milestones_url":"https://api.github.com/repos/nathanl/ecto/milestones{/number}","notifications_url":"https://api.github.com/repos/nathanl/ecto/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nathanl/ecto/labels{/name}","releases_url":"https://api.github.com/repos/nathanl/ecto/releases{/id}","created_at":"2015-12-31T13:24:26Z","updated_at":"2015-12-31T13:24:29Z","pushed_at":"2016-01-08T15:12:23Z","git_url":"git://github.com/nathanl/ecto.git","ssh_url":"git@github.com:nathanl/ecto.git","clone_url":"https://github.com/nathanl/ecto.git","svn_url":"https://github.com/nathanl/ecto","homepage":"","size":6053,"stargazers_count":0,"watchers_count":0,"language":"Elixir","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":"elixir-lang:master","ref":"master","sha":"3466496eb5ede093575c7e1544de2a591d3b87c1","user":{"login":"elixir-lang","id":1481354,"avatar_url":"https://avatars.githubusercontent.com/u/1481354?v=3","gravatar_id":"","url":"https://api.github.com/users/elixir-lang","html_url":"https://github.com/elixir-lang","followers_url":"https://api.github.com/users/elixir-lang/followers","following_url":"https://api.github.com/users/elixir-lang/following{/other_user}","gists_url":"https://api.github.com/users/elixir-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/elixir-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elixir-lang/subscriptions","organizations_url":"https://api.github.com/users/elixir-lang/orgs","repos_url":"https://api.github.com/users/elixir-lang/repos","events_url":"https://api.github.com/users/elixir-lang/events{/privacy}","received_events_url":"https://api.github.com/users/elixir-lang/received_events","type":"Organization","site_admin":false},"repo":{"id":10649374,"name":"ecto","full_name":"elixir-lang/ecto","owner":{"login":"elixir-lang","id":1481354,"avatar_url":"https://avatars.githubusercontent.com/u/1481354?v=3","gravatar_id":"","url":"https://api.github.com/users/elixir-lang","html_url":"https://github.com/elixir-lang","followers_url":"https://api.github.com/users/elixir-lang/followers","following_url":"https://api.github.com/users/elixir-lang/following{/other_user}","gists_url":"https://api.github.com/users/elixir-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/elixir-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elixir-lang/subscriptions","organizations_url":"https://api.github.com/users/elixir-lang/orgs","repos_url":"https://api.github.com/users/elixir-lang/repos","events_url":"https://api.github.com/users/elixir-lang/events{/privacy}","received_events_url":"https://api.github.com/users/elixir-lang/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/elixir-lang/ecto","description":"A database wrapper and language integrated query for Elixir","fork":false,"url":"https://api.github.com/repos/elixir-lang/ecto","forks_url":"https://api.github.com/repos/elixir-lang/ecto/forks","keys_url":"https://api.github.com/repos/elixir-lang/ecto/keys{/key_id}","collaborators_url":"https://api.github.com/repos/elixir-lang/ecto/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/elixir-lang/ecto/teams","hooks_url":"https://api.github.com/repos/elixir-lang/ecto/hooks","issue_events_url":"https://api.github.com/repos/elixir-lang/ecto/issues/events{/number}","events_url":"https://api.github.com/repos/elixir-lang/ecto/events","assignees_url":"https://api.github.com/repos/elixir-lang/ecto/assignees{/user}","branches_url":"https://api.github.com/repos/elixir-lang/ecto/branches{/branch}","tags_url":"https://api.github.com/repos/elixir-lang/ecto/tags","blobs_url":"https://api.github.com/repos/elixir-lang/ecto/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/elixir-lang/ecto/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/elixir-lang/ecto/git/refs{/sha}","trees_url":"https://api.github.com/repos/elixir-lang/ecto/git/trees{/sha}","statuses_url":"https://api.github.com/repos/elixir-lang/ecto/statuses/{sha}","languages_url":"https://api.github.com/repos/elixir-lang/ecto/languages","stargazers_url":"https://api.github.com/repos/elixir-lang/ecto/stargazers","contributors_url":"https://api.github.com/repos/elixir-lang/ecto/contributors","subscribers_url":"https://api.github.com/repos/elixir-lang/ecto/subscribers","subscription_url":"https://api.github.com/repos/elixir-lang/ecto/subscription","commits_url":"https://api.github.com/repos/elixir-lang/ecto/commits{/sha}","git_commits_url":"https://api.github.com/repos/elixir-lang/ecto/git/commits{/sha}","comments_url":"https://api.github.com/repos/elixir-lang/ecto/comments{/number}","issue_comment_url":"https://api.github.com/repos/elixir-lang/ecto/issues/comments{/number}","contents_url":"https://api.github.com/repos/elixir-lang/ecto/contents/{+path}","compare_url":"https://api.github.com/repos/elixir-lang/ecto/compare/{base}...{head}","merges_url":"https://api.github.com/repos/elixir-lang/ecto/merges","archive_url":"https://api.github.com/repos/elixir-lang/ecto/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/elixir-lang/ecto/downloads","issues_url":"https://api.github.com/repos/elixir-lang/ecto/issues{/number}","pulls_url":"https://api.github.com/repos/elixir-lang/ecto/pulls{/number}","milestones_url":"https://api.github.com/repos/elixir-lang/ecto/milestones{/number}","notifications_url":"https://api.github.com/repos/elixir-lang/ecto/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/elixir-lang/ecto/labels{/name}","releases_url":"https://api.github.com/repos/elixir-lang/ecto/releases{/id}","created_at":"2013-06-12T18:42:06Z","updated_at":"2016-01-08T05:51:00Z","pushed_at":"2016-01-08T15:12:23Z","git_url":"git://github.com/elixir-lang/ecto.git","ssh_url":"git@github.com:elixir-lang/ecto.git","clone_url":"https://github.com/elixir-lang/ecto.git","svn_url":"https://github.com/elixir-lang/ecto","homepage":"","size":7033,"stargazers_count":1359,"watchers_count":1359,"language":"Elixir","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":300,"mirror_url":null,"open_issues_count":25,"forks":300,"open_issues":25,"watchers":1359,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/elixir-lang/ecto/pulls/1180"},"html":{"href":"https://github.com/elixir-lang/ecto/pull/1180"},"issue":{"href":"https://api.github.com/repos/elixir-lang/ecto/issues/1180"},"comments":{"href":"https://api.github.com/repos/elixir-lang/ecto/issues/1180/comments"},"review_comments":{"href":"https://api.github.com/repos/elixir-lang/ecto/pulls/1180/comments"},"review_comment":{"href":"https://api.github.com/repos/elixir-lang/ecto/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/elixir-lang/ecto/pulls/1180/commits"},"statuses":{"href":"https://api.github.com/repos/elixir-lang/ecto/statuses/464165a5dd7811324cf39641ef79cbfca245f8f4"}}}}