我正在建立一个laravel项目(我是初学者)来管理客户(一个客户有很多合同,而合同有很多不同的产品)。我想实现CRUD方法并生成pdf,但是在迁移数据库时,开始时就有一个问题。 (这是开始,所以我想这对我来说很难...)
我编写了所有迁移文件(客户+合同+产品)并进行了迁移 客户(客户):
.cdk-overlay-container{
z-index:2000 !important; //higher then fixed header z-index so it comes above
}
合同(矛盾)
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Clients extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('clients', function (Blueprint $table) {
$table->Increments('id');
$table->string('name');
$table->string('company');
$table->string('email', 50);
$table->string('phone', 15);
$table->string('adress1', 50);
$table->string('adress2', 50);
$table->string('adresse3', 50);
$table->string('pays', 20);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
schema::drop('clients');
}
}
和产品(产品)
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Contrats extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contrats', function (Blueprint $table) {
$table->Increments('id');
/* $table->unsignedBigInteger('clients_name');
$table->foreign('name')->references('name')->on('Clients'); */
$table->string('id_dossier');
$table->string('id_contrat');
$table->string('id_bateau');
$table->date('startdate');
$table->index('startdate');
$table->date('enddate');
$table->index('enddate');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
schema::drop('contrats');
}
}
迁移表已成功创建。 迁移:2014_10_12_000000_create_users_table 迁移时间:2014_10_12_000000_create_users_table(0.03秒) 迁移:2014_10_12_100000_create_password_resets_table 迁移时间:2014_10_12_100000_create_password_resets_table(0.02秒) 迁移:2019_10_15_131435_clients 已迁移:2019_10_15_131435_clients(0.01秒) 迁移:2019_10_15_131454_contrats
Illuminate \ Database \ QueryException:SQLSTATE [42000]:语法错误或访问冲突:1072表中不存在键列'name'(SQL:alter table
在<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class Produits extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('produits', function (Blueprint $table) { $table->Increments('id'); $table->foreign('name'); $table->string('description', 20); $table->double('price'); $table->double('taxes'); }); } /** * Reverse the migrations. * * @return void */ public function down() { schema::drop('produits'); } }
添加约束contrats
外键(contrats_name_foreign
)引用name
(Clients
))/Applications/MAMP/htdocs/test04/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664 660 | //如果在尝试运行查询时发生异常,我们将格式化错误 661 | //包含SQL绑定的消息,这将使此异常成为 662 | //对开发人员有更多帮助,而不仅仅是数据库的错误。 663 | catch(例外$ e){
664 |抛出新的QueryException( 665 | $ query,$ this-> prepareBindings($ bindings),$ e 666 | ); 667 | } 668 |
异常跟踪:
name
我希望可以顺利进行此项目
预先感谢您的帮助,欢呼。
答案 0 :(得分:0)
外键列格式必须与它在另一张表上引用的列的格式匹配,因此它应为unsignedBigInteger
而不是string
Schema::create('contrats', function (Blueprint $table) {
$table->Increments('id');
/* $table->unsignedBigInteger('clients_name'); */
$table->string('name');
$table->foreign('name')->references('name')->on('clients');
$table->string('id_dossier');
$table->string('id_contrat');
$table->string('id_bateau');
$table->date('startdate');
$table->index('startdate');
$table->date('enddate');
$table->index('enddate');
$table->timestamps();
});
强烈建议您改为引用主键id
Eloquent将使用命名约定来使用不太明确的代码建立适当的关系
clients
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateClientsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('clients', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('company');
$table->string('email', 50);
$table->string('phone', 15);
$table->string('adress1', 50);
$table->string('adress2', 50);
$table->string('adresse3', 50);
$table->string('pays', 20);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('clients');
}
}
contrats
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateContratsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contrats', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('client_id');
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$table->string('id_dossier');
$table->string('id_contrat');
$table->string('id_bateau');
$table->date('startdate');
$table->index('startdate');
$table->date('enddate');
$table->index('enddate');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contrats');
}
}
produits
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProduitsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('produits', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('client_id');
$table->foreign('client_id')->references('id')->on('clients');
$table->string('description', 20);
$table->double('price');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('produits');
}
}
希望这会有所帮助
勇气:)
答案 1 :(得分:0)
将您的函数编写为这样的约束迁移
Schema::create('contrats', function (Blueprint $table) {
$table->Increments('id');
$table->string('id_dossier');
$table->string('id_contrat');
$table->string('id_bateau');
$table->date('startdate')->index();
$table->date('enddate')->index();
$table->timestamps();
});