2017-08-12 26 views
0

嘿,我有一個軌方案和我試過在MySQL中創建。我一個數據庫要realod從方案數據庫使用此命令:Mysql2 ::錯誤:「列名」無效的默認值

rails db:schema:load 

其他表成功創建,但該表具有默認值有問題:

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 

我得到這個錯誤:

rails aborted! 
ActiveRecord::StatementInvalid: Mysql2::Error: Invalid default value for 'legal_columns_order': CREATE TABLE `settings` 

你有什麼想法嗎?

回答

2

t.string創建一個VARCHAR(255)列,最大長度爲255個字符。但是您的默認值是269個字符。見NATIVE_DATABASE_TYPES在Rails的源代碼:

所有的
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字符的限制是在應用程序的情況下是可行的。它看起來不像,但可能使用更短的默認值是一個選項。

當你需要保存較長的文本,你有兩種選擇:不是t.string支持較長的文本(默認爲65535,但可以被配置爲數據儲存MBS)

  1. 使用t.text。但不幸的是,這種數據類型不支持默認值(BLOB and TEXT columns cannot have DEFAULT values.)。您將不得不在模型中設置默認值。

  2. 或者你手動設置更高的限制 - 例如2048個字符(明智的選擇) - 該列:

    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,幾何或JSON列「legal_columns_order」可以」 t有默認值 –

+0

好的,所以文本不能有默認值:D – Ursus

+0

那麼我該怎麼辦? –