This blog is part of our Rails 6 series. Rails 6.0 was recently released.

Rails 6 allows spaces in tables names in PostgreSQL. Before Rails 6, if we try to create a table named as user reviews, Rails tries to create a table named as reviews in schema named as user.

Let’s checkout how it works.

Rails 5.2

Let’s create a table user reviews in Rails 5.2.

>> class CreateUserReviews < ActiveRecord::Migration[5.2]
>>   def change
>>     create_table 'user reviews' do |t|
>>       t.string :value
>>
>>       t.timestamps
>>     end
>>   end
>> end

=> :change

>> CreateUserReviews.new.change
-- create_table("user reviews")
CREATE TABLE "user"."reviews" ("id" bigserial primary key, "value" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)

=> Traceback (most recent call last):
        2: from (irb):10
        1: from (irb):3:in 'change'
ActiveRecord::StatementInvalid (PG::InvalidSchemaName: ERROR:  schema "user" does not exist)
LINE 1: CREATE TABLE "user"."reviews" ("id" bigserial primary key, "...
                     ^
: CREATE TABLE "user"."reviews" ("id" bigserial primary key, "value" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)

We can see that Rails 5.2 raised an exception and tried to create table named as reviews in user schema.

Rails 6.0.0.beta2

Now, let’s create a table user reviews in Rails 6.

>> class CreateUserReviews < ActiveRecord::Migration[6.0]
>>   def change
>>     create_table 'user reviews' do |t|
>>       t.string :value
>>
>>       t.timestamps
>>     end
>>   end
>> end

=> :change

>> CreateUserReviews.new.change
-- create_table("user reviews")
CREATE TABLE "user reviews" ("id" bigserial primary key, "value" character varying, "created_at" timestamp(6) NOT NULL, "updated_at" timestamp(6) NOT NULL)

=> #<PG::Result:0x00007f9d633c5458 status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=0>

Now, we can see that the SQL generated is correct and Rails successfully created a table named as user reviews.

Here is the relevant pull request.