fguillen
fguillen

Reputation: 38772

Rails 6, ActiveRecord and Mysql 8, how set the length of a string field to 512 chars?

There are many examples that explains how to create text fields of different lengths but only having these options:

All the references I have found are very old (>3 years old). Is this still valid?

I want to create a string field of 512 chars. Should I go for a Text column? Looks like a waste of space. Can I crate a specific field of 512 bytes?

Upvotes: 0

Views: 1507

Answers (1)

fguillen
fguillen

Reputation: 38772

Looks like in the new versions the Mysql text fields allow more granualirity in the field size.

If I do this in a migration:

change_column :scrapers, :url, :string, limit: 512

I see this in the sql log:

ALTER TABLE `scrapers` CHANGE `url` `url` varchar(512) NOT NULL

And this in the mysql table structure:

`url` varchar(512) COLLATE utf8mb4_bin NOT NULL,

Upvotes: 1

Related Questions