# hasMany

## Defining

A `hasMany` relationship is a `one-to-many` relationship. For instance, a `User` may have multiple `Posts`.

```javascript
// User.cfc
component extends="quick.models.BaseEntity" {

    function posts() {
       return hasMany( "Post" );
    }

}
```

The first value passed to `hasMany` is a WireBox mapping to the related entity.

Quick determines the foreign key of the relationship based on the entity name and key values. In this case, the `Post` entity is assumed to have a `userId` foreign key. You can override this by passing a foreign key in as the second argument:

```javascript
return hasMany("Post", "FK_userID");
```

If your parent entity does not use `id` as its primary key, or you wish to join the child entity to a different column, you may pass a third argument to the `belongsTo` method specifying your parent table's custom key.

```javascript
return hasMany("Post", "FK_userID", "relatedPostId");
```

The inverse of `hasMany` is also [`belongsTo`](/2.0.0/relationships/relationship-types/belongsto.md).

```javascript
// Post.cfc
component extends="quick.models.BaseEntity" {

    function user() {
        return belongsTo( "User" );
    }

}
```

## Inserting & Updating

There are two ways to add an entity to a `hasMany` relationship. Both mirror the [insert API](/2.0.0/getting-started/creating-new-entities.md) for entities.

### save

You can call the `save` method on the relationship passing in an entity to relate.

```javascript
var post = getInstance( "Post" ).fill( {
    "title" = "My Post",
    "body" = "Hello, world!"
} );

var user = getInstance( "User" ).findOrFail( 1 );

user.posts().save( post );
```

This will add the `User` entity's id as a foreign key in the `Post` and save the `Post` to the database.

> **Note:** the `save` method is called on the `posts` relationship, not the `getPosts` collection.

### create

Use the `create` method to create and save a related entity directly through the relationship.

```javascript
var user = getInstance( "User" ).findOrFail( 1 );

user.posts().create( {
    "title" = "My Post",
    "body" = "Hello, world!"
} );
```

This example will have the same effect as the previous example.

## Removing

Removing a `hasMany` relationship is handled in two ways: either by using the `dissociate` method on the [`belongsTo`](/2.0.0/relationships/relationship-types/belongsto.md) side of the relationship or by deleting the [`belongsTo`](/2.0.0/relationships/relationship-types/belongsto.md) side of the relationship.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://quick.ortusbooks.com/2.0.0/relationships/relationship-types/hasmany.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
