"Cannot truncate a table referenced in a foreign key constraint" 外部キー制約でtruncate tableできなかった時の対応
あんまりtruncate tableする機会がいままでなかったのですが、mysqlでtruncateしようとしたら、外部キーの制約があるからできませんってエラーが出てきたとき、どうしてもtruncateしたい場合は次の様に外部キーチェックを一度キルトいいようです。
mysql> truncate table messages; ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (`database_name`.`table_name`, CONSTRAINT `foreign_key_name` FOREIGN KEY (`foreign_key_culumn_name`) REFERENCES `database_name`.`target_table_name` (`id`))
mysql> set foreign_key_checks = 0; mysql> truncate table table_name; mysql> set foreign_key_checks = 1;
migrate時にエラーメッセージが表示されていなくても、エラーになる場合
laravel 初心者の備忘録です。
php artisan migrate
してみたら、migrateは走って
migrated xxxxxxxx migrated xxxxxxxx
って表示されて特にエラーは表示されていないのに、エラーになることがありました。
とりあえずログ表示を増やそうと思ったので、コマンドを確かめました。
php artisan migrate --help Usage: migrate [options] Options: --database[=DATABASE] The database connection to use. --force Force the operation to run when in production. --path[=PATH] The path of migrations files to be executed. --pretend Dump the SQL queries that would be run. --seed Indicates if the seed task should be re-run. --step Force the migrations to be run so they can be rolled back individually. -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question --env[=ENV] The environment the command should run under. -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Help: Run the database migrations
-vvv
ってやれば一番詳しく出るんですね。
というわけでで詳しくログをだしてみることができました。
php artisan migrate -vvv
Laravelでtest用DBにmigrateする方法
Laravel入門者の備忘録です。
テスト用DBの設定を記載します。
mysqlでもpostgresでもsqliteでもなんでもいいdせうが、surfixに _testing
をつけておくとわかりやすいです。
'mysql_testing' => [ 'driver' => 'mysql', 'host' => env('TESTING_DB_HOST', 'localhost'), 'database' => env('TESTING_DB_DATABASE', 'forge'), 'username' => env('TESTING_DB_USERNAME', 'forge'), 'password' => env('TESTING_DB_PASSWORD', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ],
環境変数の設定
TESTING_DB_HOST=localhost TESTING_DB_DATABASE=homestead_testing TESTING_DB_USERNAME=homestead TESTING_DB_PASSWORD=secret
command
php artisan migrate --database=mysql_testing
参考
gulp-slim でgulpからslimに渡す変数の1文字目は小文字でないとエラーになります
gulp-slim(0.3.0)では、以下の様にgulpからslimに変数を渡せます。(dataのところです)
gulp.src("./src/slim/*.slim") .pipe(slim({ pretty: true, data: { Title: "Just a list of usernames", Users: [ { name: "Fred" }, { name: "Bill" }, { name: "Harry" } ] }, options: "encoding='utf-8'" })) .pipe(gulp.dest("./dist/html/")); });
ここのkeyの1文字目を大文字にすると以下の様なエラーがでました。
events.js:160 throw er; // Unhandled 'error' event ^ Error: no writecb in Transform class at afterTransform at TransformState.afterTransform at Socket.<anonymous> ( at emitNone (events.js:91:20) at Socket.emit (events.js:185:7) at endReadableNT (_stream_readable.js:974:12) at _combinedTickCallback (internal/process/next_tick.js:74:11) at process._tickCallback (internal/process/next_tick.js:98:9) error Command failed with exit code 1.
1文字目は小文字にしておけば、エラーがでませんでした。
NodeやGulpでhost: "0.0.0.0" を指定してサーバーを立ち上げたとき、 listen EADDRINUSE というエラーが出た場合の対応
環境
- macbook Pro(ElCapitan)
- node: v6.9.2
- gulp cli: 3.9.0
- npm: 4.0.5
状況
gulp.task("webserver.dev", () => { gulp.src("hoge") .pipe(webserver({ host: "0.0.0.0", livereload: true, port: 8080 })); });
通常これでの目的は適う様なのですが、今回は以下のエラーがでました。
listen EADDRINUSE 0.0.0.0:8080 listen EADDRINUSE 0.0.0.0:35729
悲しいことに livereloadの方も でした
原因
のエラーはこのIP, Portは使われていますよということですので、実際に確認します。
netstat
コマンドなど普段の開発フローでサーバーが立ち上がっていないことを確認します。
netstat -an
をします。
するとtcp4でみたくないものを発見しました
tcp4 0 0 *.8080 *.* LISTEN tcp4 0 0 *.35729 *.* LISTEN
ポートの8080と35729がLISTEN状態のままでした。
おそらく、立ち上げたサーバーが、開発中の何らかの理由で、ちゃんと終了されなかったということにあると思います。
対応
この問題の解決方法はいくつかあると思います。
- 別のPortを振ってあげる(暫定的な対応)
- このIP/Portを使っているプロセスをkillして、開放する
1をやっても、同じ問題が繰り返されるだけの可能性もあるので、今回は2の方法をとります。
上のPIDを探る
PIDが分かればkillすることが出来ます。(おそらく) なので、以下のコマンドをとります。
$ lsof -n -P -i :8080
するとPIDが表示されると思います。
追記
netstatでもPIDを表示できるようです。
root権限があるのであればnetstatコマンドでも-pオプションでPIDとプログラム名が表示できます。
PIDをkillする
kill {PID}
してあげればOKです。
参考までに
nodeが犯人であることが多い様です
nodeがポートを保持し続けてしまうことが原因であることが多いようです。 なので
ps aux | grep node | grep -v grep
とかしてあげると、nodeのプロセスが出てきます。 動かしているはずがないのに、生きているプロセスがあったらそいつを殺してあげるといいです。
electron系のアプリはnodeをバンバンつかっています
なお、slackアプリなどを使っている場合は、nodeのプロセスがたくさん立ち上がっていると思いますので、 killall node
はしないほうがいいと思います。
私の場合には Nginx が犯人でした
nginxでリバースプロキシをmac内に立ち上げていたのですが、そいつが悪さををしていました。nginxが正しく動いていなかったことが問題だったようです。
これも netstat
や ps
で探っていったらなんとかなりました。
References
laravel/homestead を利用する際に、Vagrant の box のバージョン遡って導入した手順の例
前置き
LaravelをMacに入れようとして、PHPのバージョンやMySQLを調整していたら、バグがひどくなってきて既存の開発環境を壊しかねなかったので、Vagrantを使って、環境を分けることにしました。
laravel/homestead
laravelにはlaravel/homesteadという便利なboxが既にあって、公式で取り上げられているのでこれをつかいます
問題
laravel/homesteadの最新はv0.6.0で、PHPは7を使っています。詳しい情報をたどるのは結構手間ですが、この辺から、バージョンを遡ってみたりしいきました。
PHPは5.6を使いたいので、v0.3.3を使う必要があります。
$ vagrant box add laravel/homestead --box-version 0.3.3 # とにかく時間がかかるのでひたすら待ちます ==> box: Loading metadata for box 'laravel/homestead' box: URL: https://atlas.hashicorp.com/laravel/homestead This box can work with multiple providers! The providers that it can work with are listed below. Please review the list and choose the provider you will be working with. 1) virtualbox 2) vmware_desktop Enter your choice: 1 ==> box: Adding box 'laravel/homestead' (v0.3.3) for provider: virtualbox box: Downloading: https://atlas.hashicorp.com/laravel/boxes/homestead/versions/0.3.3/providers/virtualbox.box ==> box: Successfully added box 'laravel/homestead' (v0.3.3) for 'virtualbox'! $ vagrant box list | grep homestead laravel/homestead (virtualbox, 0.3.3)
Install
Download
githubからcloneするなら
$ git clone https://github.com/laravel/homestead.git Homestead
もしくは以下からダウンロード
https://github.com/laravel/homestead/releases
Create Homestead.yaml
$ bash init.sh
~/.homestead
以下にできます
Vagrant立ち上げ
Homestead.yamlをいい感じに設定
ip: "192.168.10.10" memory: 2048 cpus: 1 provider: virtualbox authorize: ~/.ssh/id_rsa.pub keys: - ~/.ssh/id_rsa folders: - map: ~/Homestead to: /home/vagrant/Code type: "nfs" sites: - map: homestead.app to: /home/vagrant/Code/Laravel/public databases: - homestead variables: - key: APP_ENV value: local # blackfire: # - id: foo # token: bar # client-id: foo # client-token: bar # ports: # - send: 93000 # to: 9300 # - send: 7777 # to: 777 # protocol: udp
homestead.yamlにbox versionを記述
provider: virtualbox version: "0.3.3"
/etc/hosts
192.168.10.10 homestead.app
vagrant up
$ vagrant up