![]() Like I said before, I’m already using integers in most of my applications. ![]() And using integers as the primary key gives you all the space and performance you would expect with them, without the risk of collisions or security exploits. NanoIDs are super customizable, letting you set what length you want and what characters you want included, all while being collision resistant. This is a best-of-both-worlds approach that give the advantages of integers and the uniqueness of UUIDs, without the cons of either. There are a few reasons they chose NanoID for this task, which they outline in their blog post. To populate this column, they’re using NanoIDs. This column was added to any model that was going to be exposed to a client. How the team at PlanetScale went about this was using a BigInt as their primary key and adding a second unique column to their tables called public_id. I started researching different approaches for primary keys and eventually found the PlanetScale article, where they explained their approach when building their schema for their API. This was working great, until it got really cumbersome to maintain. I do, however, try to avoid exposing my integer IDs in my URLs and rely on unique slugs instead. In my projects, I tend to use integers simply because the projects I work aren’t particularly complex with their database setups. They’re also less readable than integers are, which can make reasoning with them harder. If storage space is a concern, UUIDs may be a bad choice. UUIDs are 128 bits long, which are twice the size of using BigInt and four times the size of using a normal Int. UUIDs do have a major downside though: their size. They’re also not sequential, meaning that you can’t guess other IDs based on a single value you have. Because of how they’re generated, it’s very easy to merge and move data between databases without the risk of conflicts. UUID stands for universally unique identifier and solves the mains issues with integers. This gives a strong argument for using UUIDs. Changing that ID in a request to your API could fetch data that you’re not supposed to have access to. Another issue is a security concern where if you have, say, an ID of 69, you can reason that there is a record with an ID of 68 and potentially one with an ID of 70. You could regenerate the data, but you could end up with key-issues on your client. One major one is that if you’re trying to merge data from two databases, you may end up with primary key conflicts. That being said, they do present some issues. For most projects, they work perfectly fine. They’re simple to work with and don’t take up a lot of space. Integers one of the most used types for primary keys. In this article, we’ll talk about PlanetScale’s approach to unique identifiers, why I went with it, and how I implement it in Prisma for my NextJS projects. And if anybody knows what they’re doing with database keys, it’s the team over at PlanetScale. I stumbled across an article from the PlanetScale team on why they use NanoIDs for their unique identifier in their APIs. With all these options to choose from, picking a PK can be overwhelming. Should you pick the tried-and-true int or go with the scalable UUID? Or maybe something like a CUID or NanoId would be a better fit for you. If you’re like me, you have a feeling of existential dread whenever you’re deciding what to choose as a primary key for your database tables. create! # "" is what we want, but "''" is what we get assert_equal "", post. stop raise end class Post < ActiveRecord:: Base end class BugTest < Minitest:: Test def test_default_reads_correctly post = Post. text :body, null: false, default: "" end end rescue StandardError, Interrupt MariaDBContainer. define do create_table :posts, force: true do | t| require "bundler/inline" gemfile ( true ) do source "" git_source ( :github ) gem "activerecord", github: :: REPO, branch: :: BRANCH gem "mysql2" end require "active_record" require "minitest/autorun" require "logger" ActiveRecord:: Base. So I've # left everything apart from "adapter" blank - I'm assuming ENV # will work to let you specify the connection details. ![]() ![]() # frozen_string_literal: true # This bug occurs at least with MariaDB 10.3, and Rails 5.2 and 6.0 # I've never made a bug report for rails before - and the documentation doesn't # say what to do re: setting up the adaptor for a mysql-specific issue.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |