5丁目通信(仮称)

とある5丁目で活動する還暦を過ぎたWebプログラマーの覚え書きです。それとかかってくる迷惑電話や、家業のアパート経営について。

#CakePHP でFixtureをbakeしたとき、enumフィールドでエラーが出たので対応した話し


でbakeでFixtureを作成したとき、モデルにenumのデータタイプがあると

Notice Error: Schema generation error: invalid column type enum('xxx','yyy','zzz') for Model2.field does not exist in DBO in [C:\xxxxxx\lib\Cake\Model\CakeSchema., line 610]

のようにエラーが出て正常にFixtureが生成できなくなります。

対応としては、FixtureをBakeするときにレコードを作成させてしまいます。

Possible Models based on your current database:
1. Model1
2. Model2
3. Model3
4. Model4
5. Model5

Enter a number from the list above,
type in the name of another model, or 'q' to exit
[q] > 2
Would you like to import schema for this fixture? (y/n)
[n] >
Would you like to use record importing for this fixture? (y/n)
[n] >
Would you like to build this fixture with data from KeywordGroup's table? (y/n)
[n] > y

ここにエラーがたくさん表示される。

Please provide a SQL fragment to use as conditions
Example: WHERE 1=1
[WHERE 1=1] >
How many records do you want to import?
[10] >

できあがったFixtureにはenumのフィールドに関しての$fieldsが抜けているので、Stringとして追加してしまいます。

public $fields = array(
:
:
'field' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 50, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
:
);

$recordsには、本番DBから適当にデータが追加されているはずです。この$recordsのデータがテストデータとしてinsertされます。

適当ですが、これでテストできました。

著:山田祥寛
¥3,366 (2024/03/22 05:53時点 | 調べ)
著:松浦 健一郎, 著:司 ゆき
¥1,430 (2024/03/28 09:58時点 | Amazon調べ)
著:谷藤賢一, 著:徳丸浩
¥2,750 (2024/03/22 05:53時点 | Amazon調べ)
ブログランキング・にほんブログ村へ 人気ブログランキング