2017-06-22 1 views
1

Avant de soumettre la question, j'ai essayé avec tous les arguments similaires de stackoverflow.com. Mais je n'ai pas trouvé le problème. J'utilise des rails 5.1.1 et MySQL 5.7 J'ai cette classe de migration appelé:Mysql2 :: Erreur: La table 'nom_table' n'existe pas: AFFICHER LES CHAMPS COMPLÈTES DE 'nom_table`

class CreateCompanies < ActiveRecord::Migration[5.1] 
    def change 
    create_table :companies do |t| 
    t.string :ragione_sociale 
    t.references :forma_giuridica, foreign_key: true 
    t.string :telefono_fisso 
    t.string :fax 
    t.string :pec 
    t.string :mail 
    t.string :web_site 
    t.string :cellulare_emergenze 
    t.string :via_legale 
    t.string :n_civico_legale 
    t.string :citta_legale 
    t.string :cap_legale 
    t.string :provincia_legale 
    t.string :regione_legale 
    t.string :via_operativa 
    t.string :n_civico_operativa 
    t.string :citta_operativa 
    t.string :cap_operativa 
    t.string :provincia_operativa 
    t.string :regione_operativa 
    t.string :pi 
    t.string :cf 
    t.string :ateco_primo 
    t.string :ateco_secondo 
    t.string :ateco_altro 
    t.timestamps 
    end 
    end 
end 

J'ai un modèle vide calledny.rb et un contrôleur appelé companies_controller.rb.

Mon database.yml est:

# MySQL. Versions 5.1.10 and up are supported. 
# 
# Install the MySQL driver 
# gem install mysql2 
# 
# Ensure the MySQL gem is defined in your Gemfile 
# gem 'mysql2' 
# 
# And be sure to use new-style password hashing: 
# http://dev.mysql.com/doc/refman/5.7/en/old-client.html 
# 
default: &default 
    adapter: mysql2 
    encoding: utf8 
    pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 
    username: root 
    password: Pipipopo09 
    host: localhost 
    port: 3300 

development: 
    <<: *default 
    database: Pluto_Demetra_development 

# 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: 
    <<: *default 
    database: Pluto_Demetra_test 

# As with config/secrets.yml, you never want to store sensitive information, 
# like your database password, in your source code. If your source code is 
# ever seen by anyone, they now have access to your database. 
# 
# Instead, provide the password as a unix environment variable when you boot 
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database 
# for a full rundown on how to provide these environment variables in a 
# production deployment. 
# 
# On Heroku and other platform providers, you may have a full connection URL 
# available as an environment variable. For example: 
# 
# DATABASE_URL="mysql2://myuser:[email protected]/somedatabase" 
# 
# You can use this database configuration with: 
# 
# production: 
#  url: <%= ENV['DATABASE_URL'] %> 
# 
production: 
    <<: *default 
    database: Pluto_Demetra_production 
    username: Pluto_Demetra 
    password: <%= ENV['PLUTO_DEMETRA_DATABASE_PASSWORD'] %> 

Lors de l'exécution rails db:migrate je l'erreur suivante:

== 20170621125622 CreateCompanies: migrating ================================== 
-- create_table(:companies) 
rails aborted! 
StandardError: An error has occurred, all later migrations canceled: 

Mysql2::Error: Table 'pluto_demetra_development.companies' doesn't exist: SHOW FULL FIELDS FROM `companies` 
C:/Users/gvieri.BSNSTRATEGIES/SitiRuby/Pluto_Demetra/db/migrate/20170621125622_create_companies.rb:3:in `change' 
bin/rails:4:in `require' 
bin/rails:4:in `<main>' 
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'pluto_demetra_development.companies' doesn't exist: SHOW FULL FIELDS FROM `companies` 
C:/Users/gvieri.BSNSTRATEGIES/SitiRuby/Pluto_Demetra/db/migrate/20170621125622_create_companies.rb:3:in `change' 
bin/rails:4:in `require' 
bin/rails:4:in `<main>' 
Mysql2::Error: Table 'pluto_demetra_development.companies' doesn't exist 
C:/Users/gvieri.BSNSTRATEGIES/SitiRuby/Pluto_Demetra/db/migrate/20170621125622_create_companies.rb:3:in `change' 
bin/rails:4:in `require' 
bin/rails:4:in `<main>' 
Mysql2::Error: Cannot add foreign key constraint 
C:/Users/gvieri.BSNSTRATEGIES/SitiRuby/Pluto_Demetra/db/migrate/20170621125622_create_companies.rb:3:in `change' 
bin/rails:4:in `require' 
bin/rails:4:in `<main>' 
Tasks: TOP => db:migrate 
(See full trace by running task with --trace) 

Je ho tu peux me aider. Merci d'avance.

Répondre

0

:: Migration [5.0] utilise int clé primaire (11)

et

:: Migration [5.1] utilise la clé primaire bigint (20)

Les raisons les plus courantes sont que lorsque creati ng une clé étrangère, le champ référencé et le champ de clé étrangère doivent correspondre:

  • Engine devrait être le même par exemple InnoDB
  • Le type de données doit être identique et de même longueur.
    par exemple. VARCHAR (20) ou INT (10) UNSIGNED
  • Collation devraient être les mêmes. par exemple. UTF8
  • unique - clé étrangère doit se référer au champ qui est unique (généralement privé) dans le tableau de référence.
4

Il est probablement dû à ActiveRecord :: Migration [5.1]. Lorsque vous souhaitez ajouter une clé étrangère au tableau companies, Migration lit d'abord les tables, mais elles n'existent pas.

Changer la migration vers 5.0 fonctionne pour moi.

class CreateCompanies < ActiveRecord::Migration[5.0] 
end 

ou supprimer des options étrangères d'abord, et ajouter manuellement la clé étrangère plus tard:

class CreateCompanies < ActiveRecord::Migration[5.1] 
    def change 
    create_table :companies do |t| 
     t.string :ragione_sociale 
     t.references :forma_giuridica # without :foreign_key option 
     # ... 
    end 

    # add foreign key here 
    add_reference :companies, :forma_giuridica 
    end 
end 
+0

C'est un travail pour moi – vierigianni