Today, we’re adding support for foreign key constraints on PlanetScale!
We understand that making database changes can be a daunting task. Now, you can use PlanetScale without reworking your existing application or data model to remove foreign key constraints. You can also use PlanetScale out of the box with your favorite ORM or framework that uses foreign key constraints by default, no changes needed.
A foreign key constraint is a database construct and an implementation that forces the foreign key relationship’s integrity (referential integrity). Namely, it ensures that a child table can only reference a parent table when the appropriate row exists in the parent table. This is not to be confused with foreign keys, which allows you to cross-reference related data across tables.
Developers should weigh the advantages and disadvantages of using foreign key constraints for their specific application, which are discussed further in the foreign key constraints documentation.
If you want a behind-the-scenes look into foreign key constraints in PlanetScale, read our blog post about the technical challenges of supporting foreign key constraints.
The new foreign key constraints support is available today in beta on a per-database level for all unsharded databases. Sharded database support will come next year. When you opt-in to the beta, we upgrade your Vitess cluster, and in some cases, we upgrade the MySQL version.
To opt-in an existing database, go to your database’s ”Settings” page and enroll on the ”Beta features” page. Note: You must be an organization administrator to enroll into beta features.
On the database’s ”Overview” page, you will see a loading spinner that says it is “Enabling foreign key constraints.” Once it no longer shows, you can use foreign key constraints in your PlanetScale database!
If you want to unenroll your database from the beta, make sure to first drop your foreign key constraints. We do not downgrade your database at this time.
Most users with newer databases can use foreign key constraints within minutes. Older databases may take longer.
If you have an existing internet-accessible MySQL or MariaDB databases that use foreign key constraints, you can also now import it into PlanetScale using our database import tool.
For more details about the beta, see the foreign key constraints beta documentation.
We have tested the beta with various popular ORMs and frameworks, but if you experience any issues with your ORM or framework of choice, let us know! While we are in this beta phase, it is helpful to hear about any problems you experience using foreign key constraints in PlanetScale. Please contact us if you have anything to share or have any questions.