[英]Haskell Persistent How to add a field to an existing table of a sql database
给定以下持久数据库定义:
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
|]
我需要将zHeight
添加到FaceDimensions
表中。 我为数据库中已有的所有现有行都提供了默认值。
当我运行runMigration migrateAll
,出现以下错误。
PersistMarshalError“字段zHeight:预期的双精度,收到:PersistText \\” z_height \\“”
为了使它起作用,我不得不手动将字段添加到sqlite数据库。 有没有办法可以直接通过Persistent做到这一点?
苏有朋:
不,没有默认值,它不起作用。 问题在于添加了新字段,其中存在现有行,其中不包含新字段的任何值。 这就是为什么我尝试使用默认值,以查看在插入新字段(列)时预先存在的行是否将获得默认值的原因。 我在这里重现了类似的错误。
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 migrationAll有一种绕过此约束的更新方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.