# Migrations

Migrations in Novel API are handled by Knex.js

{% embed url="<https://knexjs.org/guide/migrations.html>" %}

The big difference with Novel is that migrations are auto executed during runtime. This means any changes to the migrations are applied immediately.

During development time, Novel auto-generates Models and the accompanying validation and schema files to be registered within the server as well as made available to you for import.

You can check how models work below

{% embed url="<https://docs.novel.dev/novel-server/models>" %}

## Best Practices

Start by following the tutorial below

{% embed url="<https://docs.novel.dev/guides/knowledge-base/database/creating-a-table>" %}

1. Name your table in plural form.
2. Be careful of your indices and primary keys. Maybe a composite primary key is more useful.
3. There is no need to add a maximum length to strings.
4. Make use of `.references('table_name.foreign_key')` when defining relationships.
5. Make use of timestamps for boolean fields.
6. Make sure you add references to organization ID or account ID where needed.
7. Avoid using UUID v4s for your Primary Keys. Consider UUID v1/v7, ULID, or normal Big Int.

## Prototyping

When prototyping, you may perform trial-and-error on tables and columns.&#x20;

{% hint style="warning" %}
At 2025.1.0, there is no automatic way to temporarily try your database changes without data loss.&#x20;
{% endhint %}

If you need to prototype your database tables temporarily, you will need to:

1. Delete the migration file row in the `_migrations` table in your database
2. Run `knex migrate:rollback` to rollback the change that was made to the database.
3. Run the migration again with your changes.

## Changelog

* 2024-12-20 - Initial Documentation


---

# 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://docs.novel.dev/novel-server/migrations.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.
