From 117c42a9c631dfb78c8a1a4075521d31ffaf1440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B4natas=20Davi=20Paganini?= Date: Thu, 2 Dec 2021 19:27:33 -0300 Subject: [PATCH] Fix Sequelize primary key settings (#605) * Fix Sequelize primary key settings * Update timescaledb/quick-start/node.md Co-authored-by: Lana Brindley * Update timescaledb/quick-start/node.md Co-authored-by: Lana Brindley Co-authored-by: Lana Brindley Co-authored-by: Jacob Prall --- timescaledb/quick-start/node.md | 36 +++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/timescaledb/quick-start/node.md b/timescaledb/quick-start/node.md index ecdf2357f4c1..1c3b78fe511a 100644 --- a/timescaledb/quick-start/node.md +++ b/timescaledb/quick-start/node.md @@ -157,6 +157,11 @@ Connection has been established successfully. Now that we have a successful connection to the `defaultdb` database, we can build out our first database and model. + +You can skip the first two steps if you're going to use TimescaleDB cloud. The +service creates a database with the extension already enabled. + + Let's initialize Sequelize and create the necessary configuration files for our project. From the command line, type the following: @@ -237,7 +242,7 @@ Database node_test created. TimescaleDB is delivered as a PostgreSQL extension. Some instances and versions of TimescaleDB already have the extension installed. Let's make sure the -extesion is installed if it's not. +extension is installed if it's not. To start, create a database migration by running the following command: @@ -310,6 +315,29 @@ New model was created at [some path here] . New migration was created at [some path here] . ``` +Now, edit the migration file to make sure it sets up a composite primary key: + +```javascript +'use strict'; +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.createTable('page_loads', { + userAgent: { + primaryKey: true, + type: Sequelize.STRING + }, + time: { + primaryKey: true, + type: Sequelize.DATE + } + }); + }, + down: async (queryInterface, Sequelize) => { + await queryInterface.dropTable('page_loads'); + } +}; +``` + And finally, let's migrate our change and ensure that it is reflected in the database itself: @@ -338,9 +366,9 @@ Above our `app.use` statement, add the following to `index.js`: ```javascript let PageLoads = sequelize.define('page_loads', { - userAgent: Sequelize.STRING, - time: Sequelize.DATE -}); + userAgent: {type: Sequelize.STRING, primaryKey: true }, + time: {type: Sequelize.DATE, primaryKey: true } +}, { timestamps: false }); ``` You can now instantiate a `PageLoads` object and save it to the