我读过laravel雄辩的关系&数据库迁移文档,但我一件事都不懂。通过创建迁移文件,我们可以在两个表之间创建一个关系,如:
public function up()
{
Schema::create('keywords', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('keyword_group_id')
在我的laravel应用程序中,我有一张名为研究的表格,另一张叫做论文。他们有一对多的关系,每项研究都可以有一个或多个论文。在论文迁移文件中,我使用以下方法创建了foreignkey约束:
//foreign key for the research model
$table->unsignedBigInteger('research_id');
$table->index('researach_id');
现在,我想创建一个新的迁移来添加级联onDelete,这样当他们的父研究被删除时,论文就会被删除
我在一个项目中工作,其中一个表有一个名为tax_id,的外键,问题是在某些情况下创建了另一个迁移,以将tax_id更改为可空。 Schema::table('products', function (Blueprint $table) {
$table->unsignedInteger('tax_id')->nullable()->change();
}); 我意识到这一点后,我写了其他迁移,并试图运行它们,它抛出Column 'tax_id' cannot be NOT NULL,似乎在“项目”中,迁移是运行的,所以
我有个关于拉拉迁移表的问题。我在两张桌子之间有一对一的关系。OrderLine和票。我已经将这两个函数放在模型票证和OrderLine中:
public function orderline()
{
return $this->hasOne('App\OrderLine');
}
public function ticket()
{
return $this->hasOne('App\Ticket');
}
在迁移文件create_tickets_table中,架构:
public function up()
{
下面的迁移脚本在旧版本的Laravel中运行得很流畅,但我将其添加到我的全新Laravel 5.8中并运行了该脚本。我去找Error: foreign key was not formed correctly 评估迁移: public function up() {
Schema::create('evaluation', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')-
我使用类别迁移创建了类别表,然后尝试使用产品表中带有外键的categories_id到products表中的id的另一个迁移来创建products表。
请在下面找到我的移民。
Categories migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration {
/**
* Run the migrations.
*
我们如何在referenced key和foreign key中用migrations制作laravel。假设我在laravel中的数据库目录中有两个迁移文件,它们都在我的数据库中创建了一个不同的表。第一次迁移创建了一个表,用于带有Id列的posts。第二个迁移创建了一个名为post_id的列的注释表。现在,the Id column in posts table is referenced key和post_id in comments table is foreign key,我如何将这两列连接在一起?
我已经搜索了许多关于这个错误的问题,无论我尝试什么,似乎都无法修复它。这是我的错误:
语法错误或访问冲突:表中不存在1072个键列'user_id‘(SQL: alter urls添加约束urls_user_id_foreign外键(user_id)引用users (id) )
我的url表如下所示,我需要user_id列在users表上引用id
public function up()
{
Schema::create('urls', function (Blueprint $table) {
$table-&g
我想在users表中添加一个外键:
用户有一个城镇(在法语中为ville),而一个城镇可以有许多用户:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('adresse');
//un user à une seule ville
我有一个带有calls列和外键约束的user_id表。因此,user_id与users表有关。
我进行了一个迁移,删除了列和外键约束。up方法工作得很好。但是,当涉及到down时,它会崩溃,因为我添加了一个user_id列,然后恢复了约束,但是列中的值与实际数据无关,因此我得到了150个MySQL错误。
我找到了一个对我没用的解决方案。解决方案是暂时禁用外键约束。下面是我的迁移文件:
使用照明\数据库\架构\蓝图;使用照明\数据库\移动\迁移;
class RemoveUseridFromCalls extends Migration {
/**
* Run the mig
我发布了命令:
php artisan generate:scaffold order --fields="customer_id:integer(11), order_date:date, notes:text”
当它迁移时,它以错误结束:
General error: 1 table "orders" has more than one primary key (SQL: create table "orders" ("id" integer not null primary key autoincrement, "custo
我已经用CreateNewsTable迁移创建了新闻表。CreateNewsTable迁移代码是:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateNewsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('news', function (B