Edit Page

.create()

Create a record in the database.

await Something.create(initialValues);

or

Usage

#
Argument Type Details
1 initialValues Dictionary The initial values for the new record. (Note that, if this model is in "schemaful" mode, then any extraneous keys will be silently omitted.)

Note: For performance reasons, as of Sails v1.0 / Waterline 0.13, the initialValues dictionary passed into this model method will be mutated in-place in most situations (whereas in Sails/Waterline v0.12, this was not necessarily the case).

Result
#
Type Description
Dictionary? For improved performance, the created record is not provided as a result by default. But if you chain .fetch(), then the newly-created record will be sent back. (Be aware that this requires an extra database query in some adapters.)
Errors
#
Name Type When?
UsageError Error Thrown if something invalid was passed in.
AdapterError Error Thrown if something went wrong in the database adapter. See Concepts > Models and ORM > Errors for an example of how to negotiate a uniqueness error (i.e. from attempting to create a record with a duplicate that would violate a uniqueness constraint).
Error Error Thrown if anything else unexpected happens.

See Concepts > Models and ORM > Errors for examples of negotiating errors in Sails and Waterline.

Meta keys
#
Key Type Details
fetch Boolean If set to true, then the created record will be sent back.

Defaults to false.

For more information on meta keys, see .meta().

Example

#

To create a user named Finn in the database:

await User.create({name:'Finn'});

return res.ok();
Fetching the newly-created record
#
var createdUser = await User.create({name:'Finn'}).fetch();

sails.log('Finn\'s id is:', createdUser.id);

Negotiating errors

#

It's important to always handle errors from model methods. But sometimes, you need to look at errors in a more granular way. To learn more about the kinds of errors Waterline returns, and for examples of how to handle them, see Concepts > Models and ORM > Errors.

Notes

#

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference