VECTOR
MySQL type with ORMs.
These docs to not provide a comprehensive list of how to use vectors with all ORMs.
Rather, small examples for several popular choices are provided (Drizzle, Prisma, Rails).
If you use a different ORM and are having trouble getting it to work with vectors, please reach out.
Also note that for now, using PlanetScale MySQL vectors with these ORMs requires either a custom type or running raw MySQL queries.
Drizzle
Here, we’ll show how you can create and insert rows into a table that has aVECTOR
column using Drizzle.
First, modify your schema.ts
file to import the appropriate items and define two new types.
The first type is for the distance functions, and the second is to represent a vector.
schema.ts
.
Prisma
Next, let’s look at what it takes to get a table with aVECTOR
column working with Prisma.
As of this writing, Prisma does not support custom types.
Until Prisma provides support, you can still use vectors in a Prisma-powered application by using the Unsupported
function in your schema.prisma
and then use raw queries to perform vector searches.
We can add a new table with a VECTOR(4)
column by adding the following to our schema.prisma
file.
prisma db push
, it will create the table with the embedding
column having type VECTOR(4)
, even though Prisma does not technically support vectors.
From here, you can use Prisma’s queryRaw
feature to run raw SQL queries.
For example, to insert a row into this table.
Ruby on Rails
Lets look at how you can work with a vector column in a Ruby on Rails application. Say you have an existing object that models rows in a table, and you are using ActiveRecord to manage the mapping between your objects and your database. For example, an object representing a tweet inapp/models/tweet.rb
.
VECTOR
column to this table in the database.
To do this, add a new migration in db/migrations
like so:
rails db:migrate
.
With the schema updated appropriately, you can run raw SQL queries via ActiveRecord to insert rows with vectors and perform search.
For example, to insert a new row with a vector, you can do something like this: