Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between rails structure.sql and schema.rb

I am aware that schema.rb file is a ruby file and it get created and modified when a rake migration is run, but what about the structure.sql.

I have seen in some projects schema.rb and in others structure.sql and in some both files, where do they configure which file to create.

What exactly is the difference between the two.

Is structure.sql generated is specific to a particular DB.

like image 782
shiva kumar Avatar asked Dec 21 '15 11:12

shiva kumar


People also ask

What is schema RB in rails?

The schema. rb serves mainly two purposes: It documents the final current state of the database schema. Often, especially when you have more than a couple of migrations, it's hard to deduce the schema just from the migrations alone. With a present schema.

What database does Rails use?

Rails defaults to using a SQLite database when creating a new project, but you can always change it later.

What does Rails DB Reset do?

db:reset: Resets your database using your migrations for the current environment. It does this by running the db:drop , db:create , db:migrate tasks. db:rollback: Rolls the schema back to the previous version, undoing the migration that you just ran. If you want to undo previous n migrations, pass STEP=n to this task.

What is the difference between a schema and a database?

Oracle: In Oracle, every user is referred to as schema and there can be multiple schemas under one DB and then same as in SQL Server, each schema can contain DB objects such as tables, view, procedures, functions etc. MySQL: In MySQL schema and database are used interchangeably, schema is referred as database.

What is the difference between a table and a database?

A table is a set of data that is organized in to rows and columns. A database contains one or more tables that actually hold the data in the database. Each table in a database has a unique name that is used to identify it. Columns in a database also have a unique name and a data type associated with it.

What is logical schema and physical schema?

Logical schema defines how entities, attributes and relations are mapped. Physical schema is a specific implementation of the aforementioned logical schema. What is a Table? A table is a set of data that is organized in to rows and columns. A database contains one or more tables that actually hold the data in the database.

What are the different types of schema?

There are three types of schema called the conceptual schema, logical schema and physical schema. Conceptual schema describes how concepts and relationships are mapped. Logical schema defines how entities, attributes and relations are mapped. Physical schema is a specific implementation of the aforementioned logical schema. What is a Table?


1 Answers

The main difference is that schema.rb is a Ruby representation of the database, and it is generally database-agnostic. structure.sql instead is a SQL representation of the database and it depends on the specific database you selected.

You only use the structure if you have specific database features that you need and that can't be represented by the schema.rb. For example, in the past some people replaced schema.rb with structure.sql in order to use PostgreSQL JSONB fields or foreign key constraints at database level.

Both features are now supported in the migrations, therefore you don't need to switch to structure.sql anymore (in these cases).

In general, I suggest you to use the schema.rb.

like image 65
Simone Carletti Avatar answered Oct 24 '22 09:10

Simone Carletti