title: Doorkeeper ActiveRecord::NotNullViolation on "application_id" (password grant)
body: <p>I'm trying to setup <a href="https://github.com/doorkeeper-gem/doorkeeper" rel="nofollow noreferrer">Doorkeeper</a> for my API (using the password grant flow) but whenever I try to retrieve an access token I'm getting the following error on my rails server:</p>
  
  <pre><code>ActiveRecord::NotNullViolation (PG::NotNullViolation: ERROR:  null value in column "application_id" violates not-null constraint
  DETAIL:  Failing row contains (1, 1, null, e24EH4dNKBNoHh7OSzzyT_7Cg4Ka52e9TB4TP-vz4aM, null, 7200, null, 2019-07-19 22:50:44.797045, , ).
  : INSERT INTO "oauth_access_tokens" ("resource_owner_id", "token", "expires_in", "created_at", "scopes") VALUES ($1, $2, $3, $4, $5) RETURNING "id"):
  </code></pre>
  
  <p>I followed Doorkeeper's <a href="https://doorkeeper.gitbook.io/guides/ruby-on-rails/getting-started" rel="nofollow noreferrer">rails getting started guide</a> except for the last step for adding associations to my User model -- I tried doing so after running into the error but that didn't help. I'm not currently using <a href="https://github.com/plataformatec/devise" rel="nofollow noreferrer">Devise</a>.</p>
  
  <p>Did I not configure something correctly? Or maybe I have to use Devise? I'm not exactly sure where the <code>application_id</code> field plays in as the documentation for doorkeeper has the page for applications left <a href="https://doorkeeper.gitbook.io/guides/concepts/application" rel="nofollow noreferrer">blank</a>.</p>
  
  <p>What I sent to the API:</p>
  
  <pre><code>grant_type: password
  username: test@user.com
  password: password
  </code></pre>
  
  <p>My doorkeeper.rb initializer file:</p>
  
  <pre><code>Doorkeeper.configure do
    # Change the ORM that doorkeeper will use (needs plugins)
    orm :active_record
  
    # This block will be called to check whether the resource owner is authenticated or not.
    resource_owner_authenticator { current_user || render(status: 401) }
  
    resource_owner_from_credentials do |_routes|
      user = User.find_by_email(params[:username].try(:downcase))
      user if user &amp;&amp; user.authenticate(params[:password])
    end
  
    grant_flows %w[password]
  end
  </code></pre>
  
  <p>My doorkeeper migration file:</p>
  
  <pre><code>class CreateDoorkeeperTables &lt; ActiveRecord::Migration[5.2]
    def change
      create_table :oauth_applications do |t|
        t.string  :name,    null: false
        t.string  :uid,     null: false
        t.string  :secret,  null: false
  
        # Remove `null: false` if you are planning to use grant flows
        # that doesn't require redirect URI to be used during authorization
        # like Client Credentials flow or Resource Owner Password.
        t.text    :redirect_uri, null: false
        t.string  :scopes,       null: false, default: ''
        t.boolean :confidential, null: false, default: true
        t.timestamps             null: false
      end
  
      add_index :oauth_applications, :uid, unique: true
  
      create_table :oauth_access_grants do |t|
        t.references :resource_owner,  null: false
        t.references :application,     null: false
        t.string   :token,             null: false
        t.integer  :expires_in,        null: false
        t.text     :redirect_uri,      null: false
        t.datetime :created_at,        null: false
        t.datetime :revoked_at
        t.string   :scopes
      end
  
      add_index :oauth_access_grants, :token, unique: true
      add_foreign_key(
        :oauth_access_grants,
        :oauth_applications,
        column: :application_id
      )
  
      create_table :oauth_access_tokens do |t|
        t.references :resource_owner, index: true
        t.references :application,    null: false
  
        # If you use a custom token generator you may need to change this column
        # from string to text, so that it accepts tokens larger than 255
        # characters. More info on custom token generators in:
        # https://github.com/doorkeeper-gem/doorkeeper/tree/v3.0.0.rc1#custom-access-token-generator
        #
        # t.text :token, null: false
        t.string :token, null: false
  
        t.string   :refresh_token
        t.integer  :expires_in
        t.datetime :revoked_at
        t.datetime :created_at, null: false
        t.string   :scopes
  
        # If there is a previous_refresh_token column,
        # refresh tokens will be revoked after a related access token is used.
        # If there is no previous_refresh_token column,
        # previous tokens are revoked as soon as a new access token is created.
        # Comment out this line if you'd rather have refresh tokens
        # instantly revoked.
        t.string   :previous_refresh_token, null: false, default: ""
      end
  
      add_index :oauth_access_tokens, :token, unique: true
      add_index :oauth_access_tokens, :refresh_token, unique: true
      add_foreign_key(
        :oauth_access_tokens,
        :oauth_applications,
        column: :application_id
      )
  
      # Uncomment below to ensure a valid reference to the resource owner's table
      # add_foreign_key :oauth_access_grants, &lt;model&gt;, column: :resource_owner_id
      add_foreign_key :oauth_access_tokens, &lt;model&gt;, column: :resource_owner_id
    end
  end
  </code></pre>
  
  <p>My user model:</p>
  
  <pre><code>class User &lt; ApplicationRecord
    before_save { email.downcase! }
    validates :name, presence: true, length: { maximum: 50 }
    # VALID_EMAIL_REGEX = (removed for this post)
    validates :email, presence: true, length: { maximum: 255 },
                      format: { with: VALID_EMAIL_REGEX },
                      uniqueness: { case_sensitive: false }
    has_secure_password
    validates :password, presence: true, length: { minimum: 6 }
  end
  </code></pre>