Skip to content

Commit

Permalink
Fix Sequelize primary key settings (github#605)
Browse files Browse the repository at this point in the history
* Fix Sequelize primary key settings

* Update timescaledb/quick-start/node.md

Co-authored-by: Lana Brindley <[email protected]>

* Update timescaledb/quick-start/node.md

Co-authored-by: Lana Brindley <[email protected]>

Co-authored-by: Lana Brindley <[email protected]>
Co-authored-by: Jacob Prall <[email protected]>
  • Loading branch information
3 people authored Dec 2, 2021
1 parent 930d990 commit 117c42a
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions timescaledb/quick-start/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<highlight type="warning">
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.
</highlight>

Let's initialize Sequelize and create the necessary configuration files for our
project. From the command line, type the following:

Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 117c42a

Please sign in to comment.