Change the Primary Key Data Type with Sequelize

We recently adjusted how we handle primary keys. Previously they were UUIDs with a max length of 40 characters. With our Declarative Sync feature, we allow developers to set primary key values from their configuration files. Thus, we needed to lengthen…


This content originally appeared on DEV Community and was authored by Sean C Davis

We recently adjusted how we handle primary keys. Previously they were UUIDs with a max length of 40 characters. With our Declarative Sync feature, we allow developers to set primary key values from their configuration files. Thus, we needed to lengthen the maximum number of characters allowed on primary keys in our database.

Seems simple, right?

I thought so, too. We're using Sequelize as our ORM tool, and I found a handy changeColumn method.

So that's what I did. It looked like this:

await migration.changeColumn(tableName, columnName, {
  type: DataTypes.STRING(191),
});

Note: In these examples, I'm accessing Sequelize methods through an object called migration. This is because we use Actionhero to run our database migrations. Your objects will look different, but the methods on them should be the same.

I first tested with SQLite and voila! It did exactly as I expected. All the primary keys were changed and working just lovely.

Changing PostgreSQL Primary Keys

Since we support both Postgres and SQLite as our application database, I moved on to test in Postgres, and that's when, instead of the database being properly migrated, I was presented with this lovely message:

column "id" is in a primary key

I thought: Yes, true. That is correct. And ... ?

It turns out Sequelize doesn't handle this action well with Postgres. After going down a rabbit hole in playing around with constraints, I ended up just writing the SQL statement directly. It looked something like this:

const query = `ALTER TABLE "${tableName}" ALTER COLUMN "${columnName}" SET DATA TYPE varchar(${maxIdLength}); `;
await migration.sequelize.query(query);

That worked!

Consistency is Awesome! (SQLite is Weird.)

It made sense to me to try to use the same approach with both databases. So I tried my Postgres solution with SQLite.

It didn't work. (Sensing a theme yet?)

That seemed odd. But, of course, we already know that SQLite is weird. And it turns out SQLite's ALTER TABLE methods are extremely (and intentionally) limited.

Which meant I was stuck with two solutions. And when that happens, we tend to look at the current dialect and execute the appropriate code. And that's why this is the weird function that alters the primary key column in both Postgres and SQLite:

const changeColumn = async (tableName, columnName) => {
  if (config.sequelize.dialect === "postgres") {
    const query = `ALTER TABLE "${tableName}" ALTER COLUMN "${columnName}" SET DATA TYPE varchar(${maxIdLength}); `;
    await migration.sequelize.query(query);
  } else {
    await migration.changeColumn(tableName, columnName, {
      type: DataTypes.STRING(191),
    });
  }
};

You can see the complete set of changes that came along with this code in this pull request.


This content originally appeared on DEV Community and was authored by Sean C Davis


Print Share Comment Cite Upload Translate Updates
APA

Sean C Davis | Sciencx (2021-05-12T15:45:43+00:00) Change the Primary Key Data Type with Sequelize. Retrieved from https://www.scien.cx/2021/05/12/change-the-primary-key-data-type-with-sequelize/

MLA
" » Change the Primary Key Data Type with Sequelize." Sean C Davis | Sciencx - Wednesday May 12, 2021, https://www.scien.cx/2021/05/12/change-the-primary-key-data-type-with-sequelize/
HARVARD
Sean C Davis | Sciencx Wednesday May 12, 2021 » Change the Primary Key Data Type with Sequelize., viewed ,<https://www.scien.cx/2021/05/12/change-the-primary-key-data-type-with-sequelize/>
VANCOUVER
Sean C Davis | Sciencx - » Change the Primary Key Data Type with Sequelize. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/12/change-the-primary-key-data-type-with-sequelize/
CHICAGO
" » Change the Primary Key Data Type with Sequelize." Sean C Davis | Sciencx - Accessed . https://www.scien.cx/2021/05/12/change-the-primary-key-data-type-with-sequelize/
IEEE
" » Change the Primary Key Data Type with Sequelize." Sean C Davis | Sciencx [Online]. Available: https://www.scien.cx/2021/05/12/change-the-primary-key-data-type-with-sequelize/. [Accessed: ]
rf:citation
» Change the Primary Key Data Type with Sequelize | Sean C Davis | Sciencx | https://www.scien.cx/2021/05/12/change-the-primary-key-data-type-with-sequelize/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.