---
title: "Rails 6 allows spaces in postgres table names"
description: "Rails 6 allows spaces in postgres table names"
canonical_url: "https://www.bigbinary.com/blog/rails-6-allows-spaces-in-postgres-table-names"
markdown_url: "https://www.bigbinary.com/blog/rails-6-allows-spaces-in-postgres-table-names.md"
---

# Rails 6 allows spaces in postgres table names

Rails 6 allows spaces in postgres table names

- Author: Amit Choudhary
- Published: June 5, 2019
- Categories: Rails 6, Rails

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.

```ruby
>> 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.

```ruby
>> 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](https://github.com/rails/rails/pull/34561).

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-allows-spaces-in-postgres-table-names)
