Is there ever reason to use string data type when using rails 6 with postgres database?

318 views Asked by At

I see here that :text data type seems to perform exactly the same as :string

This comment particularly:

PostgreSQL implementation prefers text. The only difference for pg string/text is constraint on length for string. No performance differences.

Is there any good reason to ever use :string when making a rails app using a postgresql database?

1

There are 1 answers

0
Siim Liiser On BEST ANSWER

No difference in performance, difference in semantics.

Several libraries (for example simpleform) look at the data type of the field in the database and perform differently depending on it. Simple form will add a number input if it's a number, a checkbox if it's a boolean and so on. For this case, it will add a single line text field for string and a multiline text box for text.

Since there is no difference in performance, you can use either, but it's still useful to denote semantics.