在表中我有一列“语言”,它是字符串类型并存储值,如:“en”,“ru”等。现在我需要存储多个值,例如:[“en”,“ru”]。因此,我决定创建类型为 jsonb 的新列“languageb”并删除“language”列。重要的是“language”列的当前值必须保存到“languageb”列。我的意思是,如果“language”列的值为“en”,那么“languageb”列的值必须保存为[“en”]。 所以,我尝试创建新的迁移:
Yii::$app->db->createCommand("UPDATE ".$this->tableName." SET languageb = language")->execute();
错误是:
Exception: SQLSTATE[42804]: Datatype mismatch: 7 ERROR: column "languageb" is of type jsonb but expression is of type character varying
最佳答案
在影响它之前,您必须将列语言转换为 jsonb 呈现。
尝试这样的事情:
Yii::$app->db->createCommand("UPDATE ".$this->tableName." SET languageb = language::jsonb[]")->execute();
有关转换的更多信息,请参阅 postgres 文档:https://www.postgresql.org/docs/9.4/datatype-json.html
关于php - Yii2 和 PostreSQL : How to change column type from string to jsonb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58723818/