Code Monkey home page Code Monkey logo

practice-for-week-11-sequelize-create-migration-short-practice's Introduction

Practice: Creating Migrations in Sequelize

In this short practice, you will be using Sequelize to make a migration to CREATE a table.

Getting started

Download starter.

Execute the setup-commands.sh script from the root directory of this practice to install required dependencies and setup your .env file based on the example file. View the contents of the script to see the commands that are executed, and view the results in the terminal to confirm that setup was successful.

sh setup-commands.sh

The Express endpoints for this practice will not yield results from a database but are included in order to demonstrate how to initialize Sequelize with an existing server. This can be seen from the plaintext responses given by these endpoints.

In future practices you will utilize routes in order to interact with a connected database.

Step 1: Generate a migration file

After executing the setup script, cd into the server directory.

Then, in the terminal, use sequelize-cli to generate a new migration called create-color using the command below:

npx sequelize migration:generate --name create-color 

When this step is completed you should see a new file created in the migrations directory.

Step 2: Use queryInterface to create a new table

In the migration file that was created, utilize the queryInterface to create a new table in the up key's callback function. The name of this table should be Colors.

Notice that this does not follow the naming convention that you may be familiar with from SQL. This will become important when working with models in a future lesson.

The Colors table should have the following attributes:

attribute type constraints
id integer Primary Key, Autoincrement
name string none

Step 3: Use queryInterface to drop the table

Utilize the queryInterface to drop the table in the down key's callback function. It is always good practice to create these corresponding down actions as the migration is created. This will be executed whenever undoing the migration.

Step 4: Run the migration

Use sequelize-cli to run the migration, creating the Colors table using the following command:

npx dotenv sequelize db:migrate

Step 5: Validate the table creation

Open the database with sqlite3 in the terminal.

Check which tables exist in the database with .tables. You should see the Colors table that you defined as well as the SequelizeMeta table created by Sequelize.

Validate the structure of the Colors table with .schema Colors. You should see the SQL that was executed to create the table, including the columns, types, and additional constraints. The output should look similar to:

CREATE TABLE `Colors` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(255));

Congratulations!

You are now able to create and run migrations in Sequelize that CREATE tables.

practice-for-week-11-sequelize-create-migration-short-practice's People

Contributors

aa-assessment-project-manager[bot] avatar lloydakresi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.