2017-08-12 10 views
0

ねえ、このコマンドでスキームからデータベースをrealodしたい私は、レールのスキームを持っていると私は、MySQL .Iでデータベースを作成しようとしました:Mysql2 ::エラー:「COLUMN_NAME」の無効なデフォルト値

rails db:schema:load 

他のテーブルが正常に作成されますが、デフォルト値を持って、この表には、問題があります。

rails aborted! 
ActiveRecord::StatementInvalid: Mysql2::Error: Invalid default value for 'legal_columns_order': CREATE TABLE `settings` 
:私はこのエラーを持っている

create_table "settings", force: :cascade do |t| 
    t.integer "user_id" 
    t.string "legal_columns_order",  default: "id,indicator,classification,urgency,package,registrar,subset_type,creation_time,sender,conjunctions,followings,responses,letter_receivers,letter_date,subject,letter_number,description,recipient,receiving_type,person_name,tel_number,portal_number,operator,transcriptions" 
    t.string "legal_columns_active", default: "true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true" 
    t.string "official_columns_order", default: "id,indicator,classification,urgency,recipient,package,creation_time,sender,registrar,subset_type,conjunctions,followings,responses,letter_receivers,letter_date,subject,letter_number,barcode,description,receiving_type,transcriptions" 
    t.string "official_columns_active", default: "true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true" 
    t.string "panel_column_order",  default: "{\"panel_1\":[\"letterType\",\"subject\",\"subjectSugestion\",\"letterNumber\",\"classification\",\"sender\",\"urgency\",\"receivers\",\"letterDate\"],\"panel_2\":[\"recipient\",\"recivingType\",\"creation_time\",\"Packageid\",\"barcode\",\"Scan\"],\"panel_3\":[\"following\",\"conjunction\",\"response\",\"transcriptions\",\"description\",\"enclosed\",\"person_name\",\"tel_number\",\"portal_number\",\"operator\"],\"panel_names\":{\"panel_1\":\"اطلاعات اصلی\",\"panel_2\":\"اطلاعات ثبتی\",\"panel_3\":\"اطلاعات تکمیلی\"}}" 
    t.string "package_panel_columns", default: "{\"panel_1\":[\"courier_company\",\"classification\",\"receiving_type\",\"courier_type\",\"first_barcode\",\"second_barcode\",\"post_receiving_date\",\"creation_time\",\"registrar\"],\"panel_2\":[\"sender\",\"letter_receivers\",\"recipient_unit\",\"export_date\",\"subject\",\"recipient\",\"description\"],\"panel_names\":{\"panel_1\":\"اطلاعات اصلی\",\"panel_2\":\"اطلاعات ثبتی\"}}" 
    t.string "package_columns_order", default: "id,courier_company,classification,registrar,post_receiving_date,receiving_type,courier_type,first_barcode,sender,letter_receivers,export_date,subject,recipient,description,creation_time,recipient_unit" 
    t.string "package_columns_active", default: "true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true" 
    t.string "form_panel_columns",  default: "{\"panel_1\":[\"formType\",\"sender\",\"subject\",\"receivers\",\"creationDate\",\"receptionDate\",\"registrar\"],\"panel_2\":[\"recipient\",\"recipient_unit\",\"attachments\",\"documentNumber\",\"deliveryDate\",\"description\"],\"panel_names\":{\"panel_1\":\"اطلاعات اصلی\",\"panel_2\":\"اطلاعات تکمیلی\"}}" 
    t.string "form_columns_order",  default: "id,form_type,sender,subject,registrar,form_receivers,creation_date,reception_date,recipient,document_number,delivery_date,description,recipient_unit" 
    t.string "form_columns_active",  default: "true,true,true,true,true,true,true,true,true,true,true,true,true" 
    t.string "tracking_column_order", default: "registrar,package,classification,indicator,letter_urgency,sender,subset_type,letter_created_at,assignee,paraph,tracking_type,tracking_urgency,tracking_created_at" 
    t.string "tracking_column_active", default: "true,true,true,true,true,true,true,true,true,true,true,true,true" 
    t.datetime "created_at",                                                                                                                                          null: false 
    t.datetime "updated_at",                                                                                                                                          null: false 
    t.index ["user_id"], name: "index_settings_on_user_id" 
    end 

ご存じですか?

答えて

2

t.stringは、最大長が255文字のVARCHAR(255)の列を作成します。しかし、デフォルトは269文字です。 RailsのソースコードにNATIVE_DATABASE_TYPESを参照してください:すべての

NATIVE_DATABASE_TYPES = { 
    primary_key: "bigint auto_increment PRIMARY KEY", 
    string:  { name: "varchar", limit: 255 }, 
    text:  { name: "text", limit: 65535 }, 
    integer:  { name: "int", limit: 4 }, 
    float:  { name: "float" }, 
    decimal:  { name: "decimal" }, 
    datetime: { name: "datetime" }, 
    timestamp: { name: "timestamp" }, 
    time:  { name: "time" }, 
    date:  { name: "date" }, 
    binary:  { name: "blob", limit: 65535 }, 
    boolean:  { name: "tinyint", limit: 1 }, 
    json:  { name: "json" }, 
    } 

まず、あなたは255文字の制限は、アプリケーションのコンテキストで実行可能であるかどうかを調査する必要があります。それは見た目にはなりませんが、より短いデフォルトを使用することはオプションです。

あなたは二つの選択肢持って長い文章保存する必要があります。

  1. 利用t.textの代わりに、t.string65535にデフォルトではなく、データのMBを格納するように構成することができます)長いテキストをサポートしています。しかし残念ながら、このデータ型はデフォルト(BLOB and TEXT columns cannot have DEFAULT values.)をサポートしていません。モデルのデフォルトを設定する必要があります。例えば2048の文字(賢明な選択) - -

  2. それとも、手動で上限設定、その列に:

    t.string "legal_columns_order", limit: 2_048, default: ...  
    

を私はTEXT列の型を使用することをお勧めしとでデフォルト値を処理するために、これは、デフォルトのテキストが後で変更されたときの方がはるかに簡単なためです。モデルでデフォルトを処理するには

、私はこのようなものだろう:私はこれをしなかったし、このエラーを得た

after_initialize :set_defaults 

private 
def set_defaults 
    self.legal_columns_order ||= "id,indicator,classification,urgency,package,registrar,subset_type,creation_time,sender,conjunctions,followings,responses,letter_receivers,letter_date,subject,letter_number,description,recipient,receiving_type,person_name,tel_number,portal_number,operator,transcriptions" 
    self.legal_columns_active ||= "true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true" 
    # ... 
end 
+0

私はこれをもう一度このエラー:Mysql2 ::エラー:BLOB、TEXT、GEOMETRYまたはJSON列 'legal_columns_order'デフォルト値 –

+1

@AfsaneFadaeiあなたは正しいです。私は、MySQLがTEXTカラムのデフォルトをサポートしていないことを忘れていました。私はそれに対処するために私の答えを更新しました。 – spickermann

1

文字列フィールドの最大長のサイズは255文字で、それらは269文字だと思います。文字列からテキストに移動します。

+0

を:Mysql2 ::エラー:BLOB、TEXT、GEOMETRYまたはJSONの列「legal_columns_order」することができます」デフォルト値は –

+0

ですので、テキストにはデフォルト値を設定することはできません:D – Ursus

+0

どうすればよいですか? –

関連する問題