# RSpec and PostgreSQL not playing nicely together

**URL:** <https://www.ruby-forum.com/t/rspec-and-postgresql-not-playing-nicely-together/152502>\
**Category:** RSpec\
**Created:** [November 4, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/rspec-and-postgresql-not-playing-nicely-together/152502 "2008-11-04T17:33:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 4, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/rspec-and-postgresql-not-playing-nicely-together/152502/1 "2008-11-04T17:33:17Z")

</div>

## Here is a very simple spec:

require File.expand\_path(File.dirname( **FILE** ) + ‘/…/spec\_helper’)

describe Product, “The Product model” do

describe “When a new blank product object gets created” do  
before(:each) do  
@product = Product.new  
end

```
it "should not be valid" do
  @product.should_not be_valid
  @product.errors.on(:title).should_not be_nil
  # plenty other errors to display...
end

```

## end end

## And here is the error message when I run this spec:

spec product\_spec.rb  
F

1. 

ActiveRecord::StatementInvalid in ‘Product The Product model When a new  
blank product object gets created should not be valid’  
PGError: ERROR: relation “products” does not exist  
: SELECT a.attname, format\_type(a.atttypid, a.atttypmod),  
d.adsrc, a.attnotnull  
FROM pg\_attribute a LEFT JOIN pg\_attrdef d  
ON a.attrelid = d.adrelid AND a.attnum = d.adnum  
WHERE a.attrelid = ‘products’::regclass  
AND a.attnum \> 0 AND NOT a.attisdropped  
ORDER BY a.attnum  
./product\_spec.rb:7:in `new’  
./product\_spec.rb:7:

Finished in 1.025511 seconds

## 1 example, 1 failure

I don’t understand why RSpec is trying to look for some kind of  
relation. In the spec I am simply calling Product.new

This spec worked perfectly with MySQL.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 4, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/rspec-and-postgresql-not-playing-nicely-together/152502/2 "2008-11-04T17:47:59Z")

</div>

On Tue, Nov 4, 2008 at 11:33 AM, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> –  
> ON a.attrelid = d.adrelid AND a.attnum = d.adnum
> 
> I don’t understand why RSpec is trying to look for some kind of  
> relation. In the spec I am simply calling Product.new
> 
> This spec worked perfectly with MySQL.

I am going to go out on a limb and say that I don’t think this is an  
RSpec problem. Outside of the spec, if you launch script/console in  
both development and test environments, when you do Product.new, do  
you have any issues?

–  
Zach D.  
[http://www.continuousthinking.com](http://www.continuousthinking.com)

> **[Homepage | Mutually Human](https://www.mutuallyhuman.com)**
>
> We empower businesses to overcome challenges with a holistic focus on People, Process, and Technology for lasting results and innovation.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 4, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/rspec-and-postgresql-not-playing-nicely-together/152502/3 "2008-11-04T18:07:39Z")

</div>

Doh! I forgot to run the migrations in the newly created DB.

Thanks.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 4, 2008, 6:17pm UTC](https://www.ruby-forum.com/t/rspec-and-postgresql-not-playing-nicely-together/152502/4 "2008-11-04T18:17:21Z")

</div>

> On Tue, Nov 4, 2008 at 11:33 AM, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:

> > ```
> > @product = Product.new
> > 
> > ```

> > PGError: ERROR: relation “products” does not exist

> > I don’t understand why RSpec is trying to look for some kind of  
> > relation. In the spec I am simply calling Product.new

PostgreSQL uses the term ‘relation’ for ‘table’ (hence the term  
‘relational  
database’). Some folks think that a relation is a relationship between  
one  
table and another, via a shared column value, or key. However, a  
relation is  
actually the relationship between the values of the columns in a the  
rows of  
a table. A database consists of a set of these relations, each of which  
satisfies a given logical statement, or predicate. For example, your  
products table is set of related columns and rows such that every row  
corresponds to a “product” in your system, whose column values match  
attributes of that product.

My first guess as to your problem is that you need to run ‘rake  
db:migrate’.

///ark

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 4, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/rspec-and-postgresql-not-playing-nicely-together/152502/5 "2008-11-04T20:30:47Z")

</div>

On Nov 04, 2008, at 5:07 pm, Fernando P. wrote:

> Doh! I forgot to run the migrations in the newly created DB.

Hi Fernando

I used to do this all the time, so I made a db:migrate:all task[1]  
that means you don’t have to remember which version which DB is on.  
Mine is for Merb, but I can only see a couple of lines that should  
need changing for Rails. You might find it useful.

Ashley

[1]  
[http://aviewfromafar.net/2008/10/26/adventures-in-merbddland-3-reduce-migration-pain-with-db-migrate-all-rake-tasks](http://aviewfromafar.net/2008/10/26/adventures-in-merbddland-3-reduce-migration-pain-with-db-migrate-all-rake-tasks)

–

> **[PatchSpace](http://www.patchspace.co.uk/)**

> **[影视资源聚合平台](http://www.aviewfromafar.net/)**
>
> 为您提供高清电影、电视剧、动漫和综艺内容，支持在线观看与内容筛选。

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 4, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/rspec-and-postgresql-not-playing-nicely-together/152502/6 "2008-11-04T18:52:31Z")

</div>

Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> –  
> ON a.attrelid = d.adrelid AND a.attnum = d.adnum
> 
> I don’t understand why RSpec is trying to look for some kind of  
> relation. In the spec I am simply calling Product.new
> 
> This spec worked perfectly with MySQL.

Highly doubt it’s an RSpec problem. Just looks like you didn’t create  
the table. Try “rake db:migrate db:test:prepare”

Pat

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 4, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/rspec-and-postgresql-not-playing-nicely-together/152502/7 "2008-11-04T22:10:48Z")

</div>

## Thanks Ashley. And I am sure I will forget to run migration for the test environment each time I make changes to it. I will create a dumb script that looks like:

RAILS\_ENV=production rake db:migrate  
RAILS\_ENV=test rake db:migrate  
…

It’s not DRY but it’s so easy to read with so little lines of code. Well  
it would be very easy to refactor.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 5, 2008, 12:01am UTC](https://www.ruby-forum.com/t/rspec-and-postgresql-not-playing-nicely-together/152502/8 "2008-11-05T00:01:14Z")

</div>

On Nov 04, 2008, at 10:15 pm, Mark W. wrote:

> I think it’s actually simpler to do ‘rake db:test:prepare’ rather  
> than migrate the test database. Migrations can be a pain when you’ve  
> only got one database to worry about, much less two. The  
> db:test:prepare task extracts the schema from the development  
> database, drops the test database, then builds it again with the  
> schema script. One reason I like it is that I know for sure that my  
> tests are starting from a known state - empty. The other is that I  
> know for sure that the test db matches the dev db.

Except, this doesn’t work if your migrations contain seed data, eg  
lookup tables. In which case you have to compensate for this in your  
spec setups, which is a risky DRY violation. It’s safer to get your  
test db into shape the same way you get your production one.

Ashley

–

> **[PatchSpace](http://www.patchspace.co.uk/)**

> **[影视资源聚合平台](http://www.aviewfromafar.net/)**
>
> 为您提供高清电影、电视剧、动漫和综艺内容，支持在线观看与内容筛选。

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 4, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/rspec-and-postgresql-not-playing-nicely-together/152502/9 "2008-11-04T23:17:16Z")

</div>

On Tue, Nov 4, 2008 at 1:10 PM, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## Thanks Ashley. And I am sure I will forget to run migration for the test environment each time I make changes to it. I will create a dumb script that looks like:
> 
> RAILS\_ENV=production rake db:migrate  
> RAILS\_ENV=test rake db:migrate

I think it’s actually simpler to do ‘rake db:test:prepare’ rather than  
migrate the test database. Migrations can be a pain when you’ve only got  
one  
database to worry about, much less two. The db:test:prepare task  
extracts  
the schema from the development database, drops the test database, then  
builds it again with the schema script. One reason I like it is that I  
know  
for sure that my tests are starting from a known state - empty. The  
other is  
that I know for sure that the test db matches the dev db.

///ark

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 5, 2008, 12:09am UTC](https://www.ruby-forum.com/t/rspec-and-postgresql-not-playing-nicely-together/152502/10 "2008-11-05T00:09:26Z")

</div>

On Tue, Nov 4, 2008 at 2:54 PM, Ashley M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > 
> 
> Except, this doesn’t work if your migrations contain seed data, eg lookup  
> tables. In which case you have to compensate for this in your spec setups,  
> which is a risky DRY violation. It’s safer to get your test db into shape  
> the same way you get your production one.

See my response in the other thread. 🙂 I can imagine situations in  
which  
none of the solutions I offered would apply, but if possible I would  
prefer  
them over depending on the contents of the test database.

We had a similar situation at work, where we thought we needed certain  
data  
in order to test. We had all kinds of problems keeping test and  
development  
in sync (both schema and data). A closer look showed that that data was  
not  
in fact necessary. I’m not saying that applies in all cases, of course.

///ark
