I'm trying to create a MySQL trigger via artisan migration.
DB::unprepared('
DELIMITER $$
CREATE TRIGGER cascade_courseAffinity_after_facultyAffinity
AFTER DELETE ON faculty_affinities
FOR EACH ROW
BEGIN
DELETE ca
FROM course_affinities AS ca
JOIN courses AS course1 ON ca.course1_id = course1.id
JOIN courses AS course2 ON ca.course2_id = course2.id
WHERE (course1.faculty_id = OLD.faculty1_id OR course1.faculty_id = OLD.faculty2_id)
AND (course2.faculty_id = OLD.faculty1_id OR course2.faculty_id = OLD.faculty2_id);
END
$$
');
Nevertheless when I run the migration, I get the error
SQLSTATE[]: Syntax error... near DELIMITER $$ at line 1
Help someone?
Edit 1: this are the migration files i'm using on MySQL
Create the "courses" table with the one to many relationship to faculty (faculty_id).
Schema::create('courses', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 150);
$table->integer('faculty_id')->unsigned();
$table->foreign('faculty_id')->references('id')->on('faculties');
$table->boolean('active')->default(1);
//$table->softDeletes();
});
Create the "faculties" table.
Schema::create('faculties', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 150)->unique();
$table->boolean('active')->default(1);
//$table->softDeletes();
});
Create the "course_affinities" table with the many to many relationship self relationship between courses.
Schema::create('course_affinities', function (Blueprint $table) {
$table->increments('id');
$table->integer('course1_id')->unsigned();
$table->foreign('course1_id')->references('id')->on('courses');
$table->integer('course2_id')->unsigned();
$table->foreign('course2_id')->references('id')->on('courses');
$table->boolean('active')->default(1);
//$table->softDeletes();
});
Create the "faculty_affinities" table with the many to many relationship self relationship between faculties.
Schema::create('faculty_affinities', function (Blueprint $table) {
$table->increments('id');
$table->integer('faculty1_id')->unsigned();
$table->foreign('faculty1_id')->references('id')->on('faculties');
$table->integer('faculty2_id')->unsigned();
$table->foreign('faculty2_id')->references('id')->on('faculties');
$table->boolean('active')->default(1);
//$table->softDeletes();
});
The problem is in the DELIMITER command. This command is intended to use just in MySQL Client. So, you can't use it in Laravel.
Take a time to read this post: Creating MYSQL Procedure in Laravel 4 Migrations
It's a problem very similar to yours.
Related
I have a MySQL in the following format. I want to copy the entries in this table[products] to another table [user_products]. What is the best approach to do this?
In the user_products table while inserting the records, i will also have to add a user_id which gets passed to the code from a html form
You can use mysql trigger as explained here
Here is the migration.
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
....
});
Schema::create('user_products', function (Blueprint $table) {
$table->id();
....
});
\Illuminate\Support\Facades\DB::unprepared('
CREATE TRIGGER backup_users BEFORE INSERT ON `users` FOR EACH ROW
BEGIN
INSERT INTO user_products(id,other_columns)
values(new.id, new.other_columns);
END
');
}
I have these two tables created from migrations in my application:
Schema::create('sectors', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
}
Schema::create('functions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->foreign('sector_id')->references('id')->on('sectors')->onDelete('restrict')->onUpdate('cascade');
}
So, when I delete some 'sector' row, the delete is successfull, but wasn't I supposed to get an MySQL error #1451 since the onDelete action is setted as RESTRICT? And the same happens when I delete data directly from database, via phpMyAdmin. What is wrong?
No, quite the opposite when you delete a 'sector' row that is related (as 'sector_id') to some 'function(s)' record(s), you should get the MySQL error #1451 since the onDelete action is set as RESTRICT
For more details
I have a Database with Student, Paper and payment tables and i need to get all papers that the student has already bought previously. the sample of my migrations are below-
paper
Schema::create('users', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('fname');
$table->string('lname');
$table->string('district');
$table->string('email')->unique();
$table->string('password');
-payments
Schema::create('payments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('paper_id')->unsigned()->index();
$table->foreign('paper_id')->references('id')->on('papers');
$table->float('amount');
$table->timestamps();
});
the code i have tried gives me the wrong data,
$papers = DB::table('papers')
->leftJoin('payments','payments.paper_id','=','papers.id')
->orderBy('papers.price','ASC')
->select('papers.*')
->where('papers.id','<>','payments.paper_id')
->get();
can anyone please help.
You can do it using where raw like this
DB::table('papers')->whereRaw('id Not in (Select paper_id FROM payments)')->get();
Or you can also add student id to get optimized results like this
DB::table('papers')->whereRaw('id NOT IN (Select paper_id FROM payments where stduent_id='.$student_id.')')->get();
I get this error when I try to add a new item with the many to many relationship using the BREAD creator for voyager:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in
field list is ambiguous (SQL: select id from products inner join
order_product on products.id = order_product.product_id
where order_product.order_id is null) (View:
D:\WindowsFolders\Documents\PHP\Voyager-test\vendor\tcg\voyager\resources\views\bread\edit-add.blade.php)
I have followed the documentation and not really sure where I am having the issue. To me it looks like my many to many setup should work in normal Laravel, but it throws that error when I try to add a new order item in the voyager panel. And the documentation really don't specify what the extra field in the parent table should be(where I put a comment).
Here is my order and order-pivot table;
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->text('comment')->nullable();
$table->integer('price');
$table->boolean('sent');
$table->boolean('paid');
$table->string('products'); // This thing
$table->timestamps();
});
Schema::create('order_product', function (Blueprint $table) {
$table->increments('id');
$table->integer('order_id')->unsigned();
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->integer('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products');
$table->integer('amount');
$table->timestamps();
});
}
Here is the product table:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->nullable();
$table->text('description');
$table->string('image');
$table->integer('price');
$table->string('slug')->unique();
$table->timestamps();
});
}
Here is the relationship function:
public function products()
{
return $this->belongsToMany(Product::class, 'order_product');
}
And lastly a picture from the Order BREAD part:
The MySQL error message is quite clear in this case: both products and order_product tables have id fields. The select list of the sql statement in the question simply has id, therefore MySQL cannot decide which table the id field should be selected from.
You need to prefix the field name with the table name to make it clear:
select products.id from ...
I had a similar issue and solved it by changing the relationship key from id to product_id inside the json options in Voyager Admin
After refreshing my migrations im getting issues between users migrations and account Types users table, cant figure out the problem.
Error
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'account_typ
es' already exists (SQL: create table `account_types` (`id` int unsigned no
t null auto_increment primary key, `name` varchar(50) not null, `created_at
` timestamp null, `updated_at` timestamp null) default character set utf8mb
4 collate utf8mb4_unicode_ci)
[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'account_typ
es' already exists
My Migrations
Account Types
public function up()
{
Schema::create('account_types', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 50);
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('account_types');
}
Users
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('surname', 20);
$table->string('email')->unique();
$table->string('password');
$table->string('mobilephone', 9);
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
Relation ship between the users and account types, i believe that maybe the problem is here, but still cant figure out what is wrong with the migration code.
Relation of Users and Account Types
public function up()
{
Schema::table('users', function($table) {
$table->integer('account_type_id')->unsigned();
$table->foreign('account_type_id')
->references('id')->on('account_types');
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropForeign('users_account_type_id_foreign');
$table->dropColumn('account_type_id');
});
}
Like I had many times before there occurred an error when you were migrating and you get this error because the migration script hasn't had the chance to register the last migration.
If you don't care about the data already stored drop all the tables in your database manually (even the migrations table) and rerun.
When you have data in your tables you want to keep you should check this migrations table and look at the batch column. Put every entry on 1 you want to keep and set the row and the rows below it on 0 (maybe deleting them will also work). Run php artisan migrate. When using phpmyadmin you can update these rows by query, eg: UPDATE migrations SET batch=0 WHERE migration LIKE "%create_users_table";
Hope this helps :).
I think you should try this:
first remove account_types, users from migrations table in your database.
And refresh your migrations.
Hope this works for you!