I have the following migration:
public function up()
{
Schema::create('topics_to_subscriptions', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->integer('topic_id')->unsigned();
$table->integer('subscription_id')->unsigned();
$table->foreign('topic_id')->references('id')->on('topics')->onDelete('cascade');
$table->foreign('subscription_id')->references('id')->on('subscriptions')->onDelete('cascade');
});
}
My undersatnding is that when using onDelete('cascade'), if I delete a subscription, then all associated TopicsToSubscriptions will be delete.
When I run App\Subscription::truncate(); all the subscriptions are deleted correctly from subscriptions table but no data is deleted from topics_to_subscriptions. what am I doing wrong?
You shouldn't be able to truncate a table referenced by foreign keys. I suspect your foreign keys never got applied correctly.
https://laravel.com/docs/5.4/migrations#foreign-key-constraints
public function up()
{
Schema::create('youtube_topics_to_subscriptions', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->integer('topic_id')->unsigned();
$table->integer('youtube_subscription_id')->unsigned();
$table->foreign('topic_id')->references('id')->on('youtube_topics')->onDelete('cascade');
$table->foreign('youtube_subscription_id')->references('id')->on('youtube_subscriptions')->onDelete('cascade');
});
}
Related
I'm trying to pass the foreign key but am stuck with this error Base table or view already exists: 1050 Table 'favorites' already exists"
how can I pass them all the foreign keys and the normal id like $table->bigInteger('user_id')->unsigned();
tables
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->Increments('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('product_id')->unsigned();
$table->timestamps();
});
Schema::create('favorites', function (Blueprint $table){
$table->bigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users');
$table->bigInteger('product_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('products');
});
}
public function down()
{
Schema::dropIfExists('favorites');
}
You're migrating the table twice, pass the foreign keys in the same migration
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->bigInteger('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('favorites');
}
Hope this helps
Other answers are correct. If ever you've missed the needed tasks before doing it, kindly try this one
1) Remove or delete the table you've migrated
2) In migrations table, delete the record which consists the name "favorites"
3) Copy and paste thi code inside the "up" function with this one
Schema::create('favorites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('product_id')->unsigned();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('product_id')->references('id')->on('products');
});
4) Run this command again
php artisan migrate
I wrote some migration but the foreign key's will not be added and give me the low error.
I dont see everything is suspicious.
General error : 1251 cannot add foreign key constraint (SQL:alter table 'advertising' add constraint 'advertising_category_id_foreign' foreign key ('category_id')refrences 'category' ('id') on delete restrict on update cascade)
I change id type in related migration's,but it didn't work.
I turned the id in related migration's to index(),but agane didn't work.
public function up()
{
Schema::create('advertising', function (Blueprint $table){
$table->increments('id')->index();
$table->integer('user_id')->unsigned()->index();
$table->string('ranking')->nullable();
$table->string('status')->nullable();
$table->string('name')->nullable();
$table->string('family')->nullable();
$table->string('advertising_name')->nullable();
$table->string('phone')->nullable();
$table->string('email')->nullable();
$table->string('address')->nullable();
$table->string('country-city')->nullable();
$table->integer('category_id')->unsigned()->nullable();
$table->string('week_days')->nullable();
$table->string('time_open')->nullable();
$table->string('time_close')->nullable();
$table->string('body')->nullable();
$table->string('facebook')->nullable();
$table->string('twitter')->nullable();
$table->string('instagram')->nullable();
$table->integer('service_garanti_id')->unsigned()->nullable();
$table->integer('professional_id')->unsigned()->nullable();
$table->integer('insurance_Limit_id')->unsigned()->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')->onUpdate('cascade');
$table->foreign('category_id')->references('id')->on('category')
->onDelete('restrict')->onUpdate('cascade');
$table->foreign('service_garanti_id')->references('id')
->on('service_garanti')
->onDelete('restrict')->onUpdate('cascade');
$table->foreign('professional_id')->references('id')
->on('professional')->onDelete('restrict')->onUpdate('cascade');
$table->foreign('insurance_Limit_id')->references('id')
->on('insurance_limit')
->onDelete('restrict')->onUpdate('cascade');
});
The related migration's :
public function up()
{
Schema::create('category', function (Blueprint $table){
$table->increments('id');
$table->string('body')->nullable();
$table->timestamps();
});
public function up()
{
Schema::create('service_garanti', function (Blueprint $table){
$table->increments('id')->index();
$table->string('body')->nullable();
$table->timestamps();
});
public function up()
{
Schema::create('Professional', function (Blueprint $table){
$table->increments('id')->index;
$table->string('body')->nullable();
$table->timestamps();
});
}
public function up()
{
Schema::create('Insurance_Limit', function (Blueprint $table){
$table->increments('id')->index();
$table->string('body')->nullable();
$table->timestamps();
});
}
Try changing the order of your migrations so that the tables containing the foreign values are created first (category, service_garanti, etc.) and then create the advertising table and make the key connections. The tables and columns must already exist in order for the migration to succeed.
The other option if you know the keys are correct and will be created, it to just disable the key checking at runtime by adding this to the top of your migration.
public function up(){
Schema::disableForeignKeyConstraints();
Schema::create('advertising', function (Blueprint $table){
...
}
}
I have this migration:
public function up()
{
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->timestamps();
});
}
And now I want to add a new column, and looks like this:
public function up()
{
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->string('pathlogo')->default('/');
$table->timestamps();
});
}
How can I make just an 'add column' on Laravel? I don't want to do php artisan migrate:refresh, or restart and then make again seed.
Now I have some data in DB which not exist on seed I just want to make a new column.
You need to change Schema::create to Schema::table (because you are not creating a table, just selecting it), and then your only line in that function should be:
$table->string('pathlogo')->default('/')->after('slug');
after will ensure the column is positioned how you want.
If you are still in development, ie. you don't have data in the table, you should just rollback all your migrations and edit the original.
Create a new migration and Use that as your up function:
public function up()
{
Schema::table('clients', function (Blueprint $table) {
$table->string('pathlogo')->default('/');
});
}
them migrate as normal.
I am using Laravel 5.3 and MySQL.
How can I add in Laravel foreign keys references to a primary key with two columns?
Below are my migration scripts (under the database/migrations/ directory):
primary key with two columns
public function up()
{
Schema::create('X', function (Blueprint $table) {
$table->integer('a')->unsigned();
$table->integer('b')->unsigned();
$table->primary(['a', 'b']);
$table->timestamps();
});
}
and in another,
public function up()
{
Schema::create('Y', function (Blueprint $table) {
$table->increments('k');
$table->foreign('c')->references(['a', 'b'])->on('X')->onDelete('cascade');
$table->timestamps();
});
}
However, it doesn't work so: how can I achieve that?
Use Schema::table() instead of Schema::create() when adding foreign key constraints to your database.
Below, snippets illustrating the fix:
// File name: 2016_09_28_create_x_table.php
public function up()
{
// Create table X
Schema::create('X', function (Blueprint $table) {
$table->integer('a')->unsigned();
$table->integer('b')->unsigned();
$table->primary(['a', 'b']);
$table->timestamps();
});
}
// File name: 2016_09_28_create_y_with_foreignkey_table.php
public function up()
{
// Create table Y
Schema::create('Y', function (Blueprint $table) {
$table->increments('k');
$table->integer('c')->unsigned();
$table->timestamps();
});
// Add Foreign key
Schema::table('Y', function (Blueprint $table) {
$table->foreign('c')->references('a')->on('X')->onDelete('cascade');
});
}
Remember unsigned() should be applied on the c.
This is the only way that I found to simulate composite keys and FK pointing to composite keys working in Laravel 5.3 - I miss a more compacted solution in Laravel.
Anyway, here is my code
// File name: 2016_09_28_create_x_table.php
public function up()
{
// Create table X
Schema::create('X', function (Blueprint $table) {
$table->increments('j');
$table->integer('a')->unsigned();
$table->integer('b')->unsigned();
$table->unique(['a', 'b']);
$table->timestamps();
});
}
// File name: 2016_09_28_create_y_with_foreignkey_table.php
public function up()
{
// Create table Y
Schema::create('Y', function (Blueprint $table) {
$table->increments('k');
$table->integer('c')->unsigned();
$table->timestamps();
});
// Add Foreign key
Schema::table('Y', function (Blueprint $table) {
$table->foreign('c')->references('j')->on('X')->onDelete('cascade');
});
}
Foreign key should be set for 2 columns. foreign() should get also array
public function up()
{
Schema::create('X', function (Blueprint $table) {
$table->integer('a')->unsigned();
$table->integer('b')->unsigned();
$table->primary(['a', 'b']);
$table->timestamps();
});
}
public function up()
{
Schema::create('Y', function (Blueprint $table) {
$table->increments('k');
$table->integer('a');
$table->integer('b');
$table->foreign(['a', 'b'])->references(['a', 'b'])->on('X')->onDelete('cascade');
$table->timestamps();
});
}`
I have this schema builder (I omitted the class extension and the schema down part)
public function up()
{
Schema::create('cat', function(Blueprint $table)
{
$table->bigIncrements('id');
$table->timestamps();
$table->string('path_img');
$table->softDeletes();
});
}
this:
public function up()
{
Schema::create('campo', function(Blueprint $table)
{
$table->bigIncrements('id');
$table->timestamps();
$table->string('nome');
});
}
and this:
public function up()
{
Schema::create('campo_cat', function(Blueprint $table)
{
$table->timestamps();
$table->bigInteger('id_campo')->unsigned();
$table->bigInteger('id_cat')->unsigned();
$table->primary(array('id_campo','id_cat'));
$table->foreign(array('id_campo','id_cat'))->references(array('id','id'))->on(array('campo','cat'));
});
}
What I am trying to do is to state that the table 'campo_cat' has 2 entries that are BOTH primary key and are also foreign key, referenced to cat id and campo id.
1)If I use unsigned when I migrate I get an array to string conversion error exception
2)Is unsigned needed? or I can just define:
$table->unsigned('..');
as both 1) and 2) campo_cat doesn't create
The problem seems to be related to:
$table->foreign(array('id_campo','id_cat'))
->references(array('id','id'))
->on(array('campo','cat'));
EDIT:
I managed to make it work by doing:
public function up()
{
Schema::create('campo_cat', function(Blueprint $table)
{
$table->timestamps();
$table->bigInteger('id_campo')->unsigned();
$table->bigInteger('id_cat')->unsigned();
$table->primary(array('id_campo','id_cat'));
$table->foreign('id_campo')->references('id')->on('campo');
$table->foreign('id_cat')->references('id')->on('cat');
});
}
AND add
->unsigned(); //to the primary key in the other 2 scheme
Still wonder why if i use array it throws an error.