2017-03-23 5 views
1

の既存のテーブルにフィールドを追加する方法を永続:私はFaceDimensionsテーブルにzHeightを追加する必要がハスケル以下の永続的なデータベース定義を指定されたSQLデータベース

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| 
Mount 
    name String 
    UniqueName name 
    desc String 
    deriving Show 

FaceSlope 
    slope Double 
    mountId MountId 
    MountIdForFaceSlope mountId 
deriving Show 

FaceDimensions 
    zTop Double 
    zBtm Double 
    zHeight Double default=5.0 
    leftx Double 
    lefty Double 
    rightx Double 
    righty Double 
    mountId MountId 
    MountIdForFaceDimensions mountId 
deriving Show 

CurrentMount 
    mountId MountId 
deriving Show 
|] 

。私はすでにdbにある既存の行のデフォルト値を与えました。

I runMigration migrateAll次のエラーが発生します。

PersistMarshalError "フィールドzHeight:予想ダブルは、受信: PersistText \" z_heightの\ ""

をこの作業を取得するために、私は手動でのSQLiteデータベースにフィールドを追加する必要がありました。 永続性を使ってこれを直接行う方法はありますか?

+1

デフォルト= 5.0ビットなしで動作しますか? – asg0451

+0

このエラーは、通常、間違った値を持つ既存の列があることを意味します。あなたは 'SHOW CREATE TABLE face_dimensions;'の出力を投稿できますか? – ephrion

+0

別の表にエラーを再現します。 –

答えて

0

アレック:

いいえデフォルトなしでは機能しませんでした。問題は新しいフィールドを追加することにありました。そこには、新しいフィールドの値を含まない既存の行があります。そのため、新しいフィールド(列)が挿入されたときに既存の行がデフォルト値を取得するかどうかを確認するために、デフォルトを試行しました。私はここで同様のエラーを再現しました。

share 
[mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| 
    WristDimensions 
    name String 
    UniqueWristDimensionName name 
    desc String 
    squaredOffRiserHeight Double 
    radius Double 
    power Double 
    deriving Show 
    |]                            

ここで、テーブルに既存のデータが存在するように、データを追加して行を挿入します。新しいフィールドを追加しようとしています(newField Double)。

share 
    [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| 
    WristDimensions 
    name String 
    UniqueWristDimensionName name 
    desc String 
    squaredOffRiserHeight Double 
    radius Double 
    power Double 
    newField Double 
    deriving Show 
    |] 

移行を実行すると、次のようになります。 次のエラーが表示されます。

Migrating: CREATE TEMP TABLE 
"wrist_dimensions_backup"("id" INTEGER PRIMARY KEY,"name" 
VARCHAR NOT NULL,"desc" VARCHAR NOT NULL, 
"squared_off_riser_height" REAL NOT NULL, 
"radius" REAL NOT NULL,"power" REAL NOT NULL,"new_field" REAL NOT  
NULL,CONSTRAINT "unique_wrist_dimension_name" UNIQUE ("name")) 
Migrating: INSERT INTO "wrist_dimensions_backup" 
("id","name","desc","squared_off_riser_height","radius","power") SELECT 
"id","name","desc","squared_off_riser_height","radius","power" FROM 
"wrist_dimensions" 
    ChampCad-exe: SQLite3 returned ErrorConstraint while attempting to perform step. 

多分:runMigration migrateAllの代わりに、この制約を回避する更新方法があります。

関連する問題