I'm using Rails 5 and MySQL 5.7. I have the following in my database.yml:
default: &default
adapter: mysql2
pool: 5
encoding: utf8mb4
charset: utf8mb4
collation: utf8mb4_unicode_ci
socket: /tmp/mysql.sock
development:
<<: *default
database: [...]
username: [...]
password: [...]
I run rake db:reset to recreate my database and I run show create table users and I see CREATE TABLEusers([...]) ENGINE=InnoDB DEFAULT CHARSET=utf8.
Why does my CHARSET=utf8 and how can I fix it (make it equal utf8mb4)? Thanks.
rake db:reset is wrong. rake db:drop && rake db:create && rake db:migrate is correct.
The reset just created the tables from the already stored schema which had the wrong charset and collation. But re-creating them from the migrations created them with the new charset and collation.
Be explicit when you create a table:
CREATE TABLE users (
name VARCHAR(99) NOT NULL,
...
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4;
Another technique is to ignore the table default, and be explicit on each column:
CREATE TABLE users (
name VARCHAR(99) CHARACTER SET utf8mb4 NOT NULL,
...
) ENGINE=InnoDB;
Probably what happened is that you specified the charset in neither place, but the DEFAULT on the database was utf8.
The problem is db:reset calls db:schema:load. If your db/schema.rb file has bad encoding values, it will load up the bad stuff. You can fix your db/schema.rb to have correct encoding before running db:reset
Related
In database.yml i have this configuration:
development:
adapter: mysql2
encoding: utf8
...
But ActiveRecord::Base.connection.collation returns latin1_swedish_ci this is not what i expected, because default collation is utf8_unicode_ci. How can i get default collation in rails?
As per my knowledge you can check it on two places, remember you have also to check your mysql database. For ruby on rails you can check configuration and add as per following at end of your database.yml file
encoding: utf8mb4
collation: utf8mb4_unicode_ci
Restart server and now it should be following this configuration. Than check with following command
ActiveRecord::Base.connection.collation
Now let us check on mysql
mysql> show variables like 'collation%';
Right now I can't check these things as mysql is not setup, so will edit later to confirm you all.
I have a symfony project (2.8 version) that run the following bundles :
Sonata Admin Bundle
Sonata User Bundle
FOS User Bundle
This website is linked with a mySQL database (version 5.6).
I had issues with doctrine when I want to update my database scheme with the following command :
php app/console doctrine:schema:update --force
Because the varchar(255)type was not supported by innoDB engine and my database so I decreased all varchar to 191 length and now it's working.
My issue now is that doctrine want to generate tables (linked with acl apparently) that I cannot control like the following :
CREATE TABLE acl_classes (id INT UNSIGNED AUTO_INCREMENT NOT NULL, class_type VARCHAR(200) NOT NULL, UNIQUE INDEX UNIQ_69DD750638A36066 (class_type), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;
How can I decrease the length of class_type to apply varchar(191) ?
Thank you by advance.
You can fix is in /vendor/symfony/security-acl/Dbal/Schema.php line 70 https://github.com/symfony/security-acl/blob/master/Dbal/Schema.php#L70
$table->addColumn('class_type', 'string', array('length' => 200));
change to
$table->addColumn('class_type', 'string', array('length' => 190));
Or you can try utf8mb4 encoding:
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
Im making an application with a pre-existing database, I set up the database.yml to use the database.
database.yml
development:
adapter: mysql2
encoding: utf8
# database: ttlem_demo_development
database: ttle
pool: 5
username: root
password:
socket: /tmp/mysql.sock
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql2
encoding: utf8
database: ttlem_demo_test
pool: 5
username: root
password:
socket: /tmp/mysql.sock
production:
adapter: mysql2
encoding: utf8
database: ttlem_demo_production
pool: 5
username: root
password:
socket: /tmp/mysql.sock
I only want one table out of the database it is called account views, I try to generate a scaffold for this with all the correct fields but it tells me i need to migrate (when i render it in the browser), if i migrate i wont be able to use the existing data, is this correct? How can i make a model that will use the existing data and fields?
Thank you for all your help :)
Two things to try:...
#1 Run your scaffold without a migration so it doesn't think you're missing one.
rails g scaffold yourmodelname fieldone:fieldtype ... etc --no-migration
#2 If that doesn't work you can go the long way round but dumping and reloading with a valid schema version number
Add this db to yml to your gemfile:
gem 'yaml_db', github: 'jetthoughts/yaml_db', ref: 'fb4b6bd7e12de3cffa93e0a298a1e5253d7e92ba'
It works for either rails 3 or rails 4.
Do a schema dump of your current database so you'll get a schema.rb with a valid version number.
bundle exec rake db:schema:dump
Now that you have a valid schema dump your data.
bundle exec rake db:data:dump
Drop your database (you can do it manually using the mysql commands if you prefer or run rake db:drop)
Now recreate it with your schema file.
bundle exec rake db:schema:load
Now add back your data
bundle exec rake db:data:load
Start your server and assuming your correctly matched all your data fields in your model (so the proper strong parameters are set from your scaffold) you should be good.
In my database config I have:
adapter: mysql
database: xxx
username: xxx
password: xxx
host: localhost
encoding: 'latin1'
My table definition has: DEFAULT CHARSET=latin1
I put this at the top of my file:
# encoding: iso-8859-1
And yet still when I save my record and load it again the latin1 data has been mysteriously converted to utf8. Can someone suggest something I'm missing?
What about the columns' encoding? You have to set the connection, the tables, the columns and the program encoding/charset to the same one to have your code work properly.
Is it possible to change the charset and collation of an existing Mysql database using Rails migrations or other options ?!
What's the best way to initially configure database charset & collation ?!
Native query could be executed in rails migration:
def self.up
execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET charset_here COLLATE collation_here;"
end
And for initial configuration you can predefine charset and collation in your database.yml file, like this:
production:
adapter: mysql2
encoding: utf8mb4
collation: utf8mb4_unicode_ci