我正在做一个学校项目,需要使用lard和erd来建立我的数据库。我正在尝试使用外键进行迁移。
如何在Laravel中使用外键?
这是我的桌子:
\\\\\\\\
C:\xampp\htdocs\newapps>php artisan migrate:fresh -v
Dropped all tables successfully.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrating: 2019_01_13_151934_create_customer_orders_table
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'customer_id' doesn't exist in table (SQL: alter table `customer_orders` add constraint `customer_orders_customer_id_foreign` foreign key (`customer_id`) references `customers` (`customer_id`))
at C:\xampp\htdocs\newapps\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'customer_id' doesn't exist in table")
C:\xampp\htdocs\newapps\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
2 PDOStatement::execute()
C:\xampp\htdocs\newapps\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
3 Illuminate\Database\Connection::Illuminate\Database\{closure}("alter table `customer_orders` add constraint `customer_orders_customer_id_foreign` foreign key (`customer_id`) references `customers` (`customer_id`)", [])
C:\xampp\htdocs\newapps\vendor\laravel\framework\src\Illuminate\Database\Connection.php:657
4 Illuminate\Database\Connection::runQueryCallback("alter table `customer_orders` add constraint `customer_orders_customer_id_foreign` foreign key (`customer_id`) references `customers` (`customer_id`)", [], Object(Closure))
C:\xampp\htdocs\newapps\vendor\laravel\framework\src\Illuminate\Database\Connection.php:624
5 Illuminate\Database\Connection::run("alter table `customer_orders` add constraint `customer_orders_customer_id_foreign` foreign key (`customer_id`) references `customers` (`customer_id`)", [], Object(Closure))
C:\xampp\htdocs\newapps\vendor\laravel\framework\src\Illuminate\Database\Connection.php:459
6 Illuminate\Database\Connection::statement("alter table `customer_orders` add constraint `customer_orders_customer_id_foreign`
foreign key (`customer_id`) references `customers` (`customer_id`
\\\\\\\\
当我删除外键时,一切正常。 现在,我收到以下错误消息:
#include <stdio.h>
#include <stdlib.h>
extern void sum_col(int n, int m, long int *matrix[], long int new_col[]);
int main() {
int n = 4, m = 3, i, j;
long int *matrix[4];
long int new_col[3];
for (i = 0; i < n; i++)
matrix[i] = (long int *)malloc(m * sizeof(long int));
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
matrix[i][j] = 100 * i + j;
sum_col(n, m, matrix, new_col);
printf("matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) printf("%8ld", matrix[i][j]);
printf("\n");
} // for
printf("new_col:\n");
for (i = 0; i < m; i++)
printf("%8ld", new_col[i]);
return 0;
} // main
答案 0 :(得分:3)
您应该在创建外键之前创建一个字段。例如,这应该起作用:
Schema::create('customer_order_products', function (Blueprint $table) {
$table->increments('customer_order_product_id');
$table->integer('order_id')->unsigned();
$table->integer('product_id')->unsigned();
$table->float('totaal_bedrag');
$table->timestamps();
$table->foreign('product_id')->references('product_id')->on('products');
$table->foreign('order_id')->references('order_id')->on('customer_orders');
});
还要确保您的迁移以正确的顺序运行。 products
和customer_orders
的迁移应在上述迁移之前运行。