title: Firebase Realtime Database - Auto Generate Primary Key
body: <p>I am writing some data to a Firebase Realtime Database.</p>
  
  <p>User Class</p>
  
  <pre><code>data class User(
      @Exclude var userId: String? = "",
      var username: String? = "",
      var email: String? = ""
  )
  </code></pre>
  
  <p><br>
  Write Function</p>
  
  <pre><code>class MainActivity : AppCompatActivity() {
  
      private lateinit var mDatabase: DatabaseReference
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          mDatabase = FirebaseDatabase.getInstance().reference
  
          bt_register_button.setOnClickListener {
              val username = et_register_username.text.toString()
              val email = et_register_email.text.toString()
  
              writeNewUser("1", username, email)
          }
  
      }
  
      private fun writeNewUser(userId: String, username: String, email: String?) {
          val user = User(username, email)
          mDatabase.child("users").child(userId).setValue(user)
      }
  
  }
  </code></pre>
  
  <p><br>
  Using the above, I can create a User record with a preset Id of 1:</p>
  
  <pre><code>
  users
       1
            email: "bill@gmail.com"
            username: "bill"
  </code></pre>
  
  <p>I obviously want the Id to be generated automatically upon creation.</p>
  
  <p>I am aware of the push() function but I'm not sure how to implement it.</p>
  
  <p><br><br>
  <strong>Edit 1</strong></p>
  
  <p>Thank you, the User key is now being set as the User UID (from Authentication):</p>
  
  <pre><code>    private fun writeNewUser(name: String, email: String?) {
          val user = User(name, email)
          mDatabase.child("Users").child(mAuth.currentUser!!.uid).setValue(user)
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/rsME5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rsME5.png" alt="Authentication + Write Database Result"></a></p>