# How to create a table if not exists?

**URL:** <https://www.ruby-forum.com/t/how-to-create-a-table-if-not-exists/179295>\
**Category:** Rails\
**Created:** [November 21, 2009, 10:44am UTC](https://www.ruby-forum.com/t/how-to-create-a-table-if-not-exists/179295 "2009-11-21T10:44:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 21, 2009, 10:44am UTC](https://www.ruby-forum.com/t/how-to-create-a-table-if-not-exists/179295/1 "2009-11-21T10:44:03Z")

</div>

Dear All

how to create a table if not exists already?  
how can i check that whether the table exists or not?

pls guide me on this

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 23, 2009, 5:27am UTC](https://www.ruby-forum.com/t/how-to-create-a-table-if-not-exists/179295/2 "2009-11-23T05:27:07Z")

</div>

Hi

```
    You will get table names for your environment with

```

ActiveRecord::Base.connection.tables

Sijo K George

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 23, 2009, 7:24am UTC](https://www.ruby-forum.com/t/how-to-create-a-table-if-not-exists/179295/3 "2009-11-23T07:24:11Z")

</div>

Newb N. wrote:

> Dear All
> 
> how to create a table if not exists already?  
> how can i check that whether the table exists or not?
> 
> pls guide me on this

Generally speaking, you shouldn’t need to do this at all. In most  
cases, the only time you should create tables is in migrations, and  
Rails takes care of the logic in there.

Why do you think you need to check whether a table exists? What are you  
trying to achieve?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
