A long-lived project that still receives updates
Rails ActiveRecord connector for Google Spanner Database



>= 6.0.0, < 7.1
 Project Readme

ActiveRecord Cloud Spanner Adapter

Google Cloud Spanner adapter for ActiveRecord.


This project provides a Cloud Spanner adapter for ActiveRecord. It supports the following versions:

  • ActiveRecord 6.0.x with Ruby 2.6 and 2.7.
  • ActiveRecord 6.1.x with Ruby 2.6 and higher.
  • ActiveRecord 7.0.x with Ruby 2.7 and higher.

Known limitations are listed in the Limitations section. Please report any problems that you might encounter by creating a new issue.


Add this line to your application's Gemfile:

gem 'activerecord-spanner-adapter'

If you would like to use latest adapter version from github then specify

gem 'activerecord-spanner-adapter', :git => 'git@github.com:googleapis/ruby-spanner-activerecord.git'

And then execute:

$ bundle


Database Connection

In Rails application config/database.yml, make the change as the following:

  adapter: "spanner"
  project: "<google project name>"
  instance: "<google instance name>"
  credentials: "<google credentails file path>"
  database: "app-dev"


To get started with Rails, read the tutorial under examples/rails/README.md.

You can also find a list of short self-contained code examples that show how to use ActiveRecord with Cloud Spanner under the directory examples/snippets. Each example is directly runnable without the need to setup a Cloud Spanner database, as all samples will automatically start a Cloud Spanner emulator in a Docker container and execute the sample code against that emulator. All samples can be executed by navigating to the sample directory on your local machine and then executing the command bundle exec rake run. Example:

cd ruby-spanner-activerecord/examples/snippets/quickstart
bundle exec rake run

NOTE: You do need to have Docker installed on your local system to run these examples.

Some noteworthy examples in the snippets directory:

  • quickstart: A simple application that shows how to create and query a simple database containing two tables.
  • migrations: Shows a best-practice for executing migrations on Cloud Spanner.
  • read-write-transactions: Shows how to execute transactions on Cloud Spanner.
  • read-only-transactions: Shows how to execute read-only transactions on Cloud Spanner.
  • bulk-insert: Shows the best way to insert a large number of new records.
  • mutations: Shows how you can use mutations instead of DML for inserting, updating and deleting data in a Cloud Spanner database. Mutations can have a significant performance advantage compared to DML statements, but do not allow read-your-writes semantics during a transaction.
  • array-data-type: Shows how to work with ARRAY data types.
  • interleaved-tables: Shows how to work with Interleaved Tables.


Limitation Comment Resolution
Interleaved tables require composite primary keys Cloud Spanner requires composite primary keys for interleaved tables. See this example for an example on how to use interleaved tables with ActiveRecord Use composite primary keys.
Lack of sequential and auto-assigned IDs Cloud Spanner doesn't autogenerate IDs and this integration instead creates UUID4 to avoid hotspotting so you SHOULD NOT rely on IDs being sorted UUID4s are automatically generated for primary keys.
Table without Primary Key Cloud Spanner support does not support tables without a primary key. Always define a primary key for your table.
Table names CANNOT have spaces within them whether back-ticked or not Cloud Spanner DOES NOT support tables with spaces in them for example Entity ID Ensure that your table names don't contain spaces.
Table names CANNOT have punctuation marks and MUST contain valid UTF-8 Cloud Spanner DOES NOT support punctuation marks e.g. periods ".", question marks "?" in table names Ensure that your table names don't contain punctuation marks.
Index with fields length add_index Cloud Spanner does not support index with fields length Ensure that your database definition does not include index definitions with field lengths.


Bug reports and pull requests are welcome on GitHub at https://github.com/googleapis/ruby-spanner-activerecord. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.


The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Activerecord::Spanner project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.