Netervey
Netervey

Reputation: 47

How to add fields to the database correctly: through a model or migrations in Sequelize?

This is my first time making a postgre database on Sequelize, and I can't figure out the migrations. As far as I understand, you have to change the database with migrations only, and you can get data through the model (probably this is not true). I want to create a RESTAPI, and I confused, how should I add/change/delete columns - through migrations or through a model? If through migrations, then I need to use the CLI for them to work, but I have no idea how to call db:migrate from express. The documentation says that all changes to the database must be done through migrations. I can not figure out what is the advantage of migrations and how to interact from the database during the execution of the server application. Thanks for answer

Upvotes: 0

Views: 171

Answers (1)

Chigozie Ijomah
Chigozie Ijomah

Reputation: 110

Changing columns is different from changing rows. I think you are referring to changing rows here. Columns refer to the properties of records while rows refer to the actual records themselves.

Migrations are typically used for changing the schema definition of tables that have already been defined (which includes but not limited to changing columns).

This may include

  • Changing the constraint of a particular column,
  • Adding a column or removing a column from a particular table,
  • Creating a new table.

Examples would be:

  • Making the email column of a Users table have the unique constraint, and
  • Removing the address column from all records in the Users table. In this case, all users will no longer have an address property/column (this will not turn it to null, but will make the column non-existent).

If you are actually talking about updating rows, deleting rows, and in general, querying for data, refer to Sequelize querying basics to understand more.

Upvotes: 1

Related Questions