Importing the sql table shows, Foreign key constraint is incorrectly formed, table made by laravel migration - laravel

In Laravel migration, all are working fine. But when I export this table and import in new db, this table shows Foreign Key constraint is incorrectly formed.
sales.sql
-- phpMyAdmin SQL Dump
-- version 4.9.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jul 05, 2020 at 08:19 PM
-- Server version: 10.4.8-MariaDB
-- PHP Version: 7.3.11
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `inventory_management`
--
-- --------------------------------------------------------
--
-- Table structure for table `sales`
--
CREATE TABLE `sales` (
`id` bigint(20) UNSIGNED NOT NULL,
`sales_no` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
`reference` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`date` date NOT NULL,
`sub_total` double NOT NULL,
`discount` double NOT NULL,
`discount_type` enum('percent','amount') COLLATE utf8mb4_unicode_ci NOT NULL,
`tax` double NOT NULL,
`tax_type` enum('percent','amount') COLLATE utf8mb4_unicode_ci NOT NULL,
`grand_total` double NOT NULL,
`payment_type` enum('no-payment','partial-payment','full-payment') COLLATE utf8mb4_unicode_ci NOT NULL,
`payment_term_id` bigint(20) UNSIGNED DEFAULT NULL,
`notes` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_by` bigint(20) UNSIGNED NOT NULL,
`updated_by` bigint(20) UNSIGNED NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`status` enum('paid','void','cancelled','overdue','unpaid') COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`customer_id` bigint(20) UNSIGNED DEFAULT NULL,
`is_item_wise_discount` enum('0','1') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
`is_item_wise_tax` enum('0','1') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `sales`
--
ALTER TABLE `sales`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `sales_sales_no_unique` (`sales_no`),
ADD KEY `sales_payment_term_id_index` (`payment_term_id`),
ADD KEY `sales_created_by_index` (`created_by`),
ADD KEY `sales_updated_by_index` (`updated_by`),
ADD KEY `sales_customer_id_index` (`customer_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `sales`
--
ALTER TABLE `sales`
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=35;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `sales`
--
ALTER TABLE `sales`
ADD CONSTRAINT `sales_created_by_foreign` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `sales_customer_id_foreign` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `sales_payment_term_id_foreign` FOREIGN KEY (`payment_term_id`) REFERENCES `payment_terms` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `sales_updated_by_foreign` FOREIGN KEY (`updated_by`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;
But all are working fine, When I alter table and do php artisan migrate. It works. The only problem is when exporting table and importing the table in new db. Thank You in advance

are you migrating just one table or the all database? if its just one table, Make sure the other tables exist in the new database with the parent table having information that the other table depending on it needs. let me say if you are migrating sales.sql and its information depends on users say( it has a users_id), then the new database must have the users table in it. Also mind about the datatypes of the column values you are relating in both tables.

Related

Laravel how to get list of related fields through pivot table

I have 4 tables : peoples, companies, countries and the pivot table company_people (as peoples & companies both belongs to many) which has both people_id and company_id.
In the People model, I have the following functions:
class People extends Model
{
// main company (only one)
public function company()
{
return $this->belongsTo(Company::class);
}
// all other companies
public function companies()
{
return $this->belongsToMany(Company::class);
}
public function country()
{
return $this->belongsTo(Country::class);
}
}
Then in the People controller, I have the following in order to prepare to display a list of all the peoples with the related main company name (only one), country name (only one) and other companies as a list of names. I can do the first 2 but not the last one. How can I do that?
$peoples = People::orderBy($sortField,$sortOrder)
->with(['companies','company','country'])
->get();
foreach ($peoples as $people) {
$people->company = '['.$people->company->company.']'; // main company name
$people->country = '['.$people->country->country.']'; // country name
$people->otherCompanies = ? // list of other company names through pivot table
}
And here all the structure of the 4 tables:
CREATE TABLE `company_people` (
`id` bigint NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`company_id` bigint UNSIGNED NOT NULL,
`people_id` bigint UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `countries` (
`id` varchar(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'AA',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`is_active` int NOT NULL DEFAULT '1',
`country` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `peoples` (
`id` bigint UNSIGNED NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`is_active` int NOT NULL DEFAULT '1',
`firstname` varchar(120) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`lastname` varchar(120) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '',
`country_id` varchar(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'ZA',
`company_id` bigint UNSIGNED DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci PACK_KEYS=0;
ALTER TABLE `company_people`
ADD PRIMARY KEY (`id`),
ADD KEY `article_id` (`company_id`),
ADD KEY `tag_id` (`people_id`);
ALTER TABLE `countries`
ADD PRIMARY KEY (`id`);
ALTER TABLE `peoples`
ADD PRIMARY KEY (`id`),
ADD KEY `country_id` (`country_id`),
ADD KEY `company_id` (`company_id`);
ALTER TABLE `company_people`
MODIFY `id` bigint NOT NULL AUTO_INCREMENT;
ALTER TABLE `peoples`
MODIFY `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `peoples`
ADD CONSTRAINT `peoples-company` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `peoples-country` FOREIGN KEY (`country_id`) REFERENCES `countries` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE `companies`
ADD CONSTRAINT `peoples-country` FOREIGN KEY (`country_id`) REFERENCES `countries` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE `company_people`
ADD CONSTRAINT `companies-peoples` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `peoples-companies` FOREIGN KEY (`people_id`) REFERENCES `peoples` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
you can use pluck() to get all the company name then toArray() to convert in array like this
$peoples = People::orderBy($sortField,$sortOrder)
->with(['companies','company','country'])
->get();
foreach ($peoples as $people) {
$people->company = '['.$people->company->company.']'; // main company name
$people->country = '['.$people->country->country.']'; // country name
$people->otherCompanies = $people->companies->pluck('name')->toArray(); // list of other company names through pivot table
}
And if you want otherCompanies name as comma seprate then use $people->companies->pluck('name')->join(',');

Oracle SQL: Missing right parenthesis

I'm migrating a database from mySQL to Oracle SQL but I'm getting a "ORA-00907: missing right parenthesis" error when creating a table. I've tried everything I can think of but still keep getting the same error.
Create table statement:
CREATE TABLE menu
(id int(11) NOT NULL AUTO_INCREMENT,
restaurant_id varchar(30) DEFAULT NULL,
menu_name varchar(30) DEFAULT NULL,
menu_description varchar(500) DEFAULT NULL,
menu_price varchar(30) DEFAULT NULL,
quantity int(11) DEFAULT '1',
PRIMARY KEY (id))
I think the problem is with the PRIMARY KEY as it's only table with PRIMARY KEYs that I get the error on. Apologies if this is an obvious question, I'm new to Oracle SQL. Thanks in advance!
Oracle != MySQL:
CREATE TABLE menu
( id number(11,0) GENERATED AS IDENTITY, --IDENTITY <=> AUTO_INCREMENT
restaurant_id varchar2(30) DEFAULT NULL, --VARCHAR2 instead of VARCHAR
menu_name varchar2(30) DEFAULT NULL,
menu_description varchar2(500) DEFAULT NULL,
menu_price varchar2(30) DEFAULT NULL,
quantity number(11,0) DEFAULT '1', --NUMBER(11,0) instead of INT(11)
PRIMARY KEY (id)
);

what's happened with my php artisan migrate

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
n your SQL syntax; check the manual that corresponds to your MySQL server v
ersion for the right syntax to use near ') on delete cascade on update casc
ade' at line 1 (SQL: alter table users add constraint users_address_id_fo
reign foreign key (address_id) references address () on delete cascade
on update cascade)
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
n your SQL syntax; check the manual that corresponds to your MySQL server v
ersion for the right syntax to use near ') on delete cascade on update casc
ade' at line 1
migrate [--bench[="..."]] [--database[="..."]] [--force] [--path[="..."]] [--pac
kage[="..."]] [--pretend] [--seed]
public function up()
{
Schema::create('users',function($table){
$table->increments('id');
$table->string('name',30);
$table->string('phone',11);
$table->integer('age');
$table->string('email',50);
$table->string('marry_status',10);
$table->integer('address_id');
$table->foreign('address_id')->reference('id')->on('address')->onDelete('cascade')->onUpdate('cascade');
$table->integer('points_id');
$table->foreign('points_id')->reference('id')->on('address')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
}
php migrate --pretend
CreateUsersTable: create table `users` (`id` int unsigned not null auto_increment primary key, `name` varchar(30) not null, `phone` varchar(11) not null, `age` int not null, `email` varchar(50) not null, `marry_status` varchar(10) not null, `address_id` int not null, `points_id` int not null, `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci
CreateUsersTable: alter table `users` add constraint users_address_id_foreign foreign key (`address_id`) references `address` () on delete cascade on update cascade
CreateUsersTable: alter table `users` add constraint users_points_id_foreign foreign key (`points_id`) references `address` () on delete cascade on update cascade
CreateAddressTable: create table `address` (`id` int unsigned not null auto_increment primary key, `users_id` int not null, `name` varchar(30) not null, `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci
CreatePointsTable: create table `points` (`id` int unsigned not null auto_increment primary key, `point` int not null, `users_id` int not null, `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci
i try but i not good
public function up()
{
Schema::create('users',function($table){
$table->increments('id');
$table->string('name',30);
$table->string('phone',11);
$table->integer('age');
$table->string('email',50);
$table->string('marry_status',10);
$table->integer('address_id');
$table->integer('points_id');
$table->timestamps();
});
Schema::table('users',function($table){
$table->foreign('points_id')->references('id')->on('address')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('address_id')->references('id')->on('address')->onDelete('cascade')->onUpdate('cascade');
});
}
You have error in your syntax. You are using reference() method, but the right method is references(). Fix it and I think everything will be ok.

Joomla migration issue

I have transferred my website to a new server after updating the files and database I am getting this issue
Table 'jimcorbe_jimcorbe.xpivg_session' doesn't exist SQL=INSERT INTO xpivg_session (session_id, client_id, time) VALUES ('18f0b96cfc5e1cb723b8405137ff36a6', 0, '1427709725')
I am very new to joomla so could not understand what it means. Can anyone provide me with the solution to get back my website live.
During the migration, it's possible that database table prefixes have changed.
Open PhpMyAdmin and look for the session table. If it doesn't exist, run the following SQL command:
CREATE TABLE IF NOT EXISTS `xpivg_session` (
`session_id` varchar(200) NOT NULL DEFAULT '',
`client_id` tinyint(3) unsigned NOT NULL DEFAULT '0',
`guest` tinyint(4) unsigned DEFAULT '1',
`time` varchar(14) DEFAULT '',
`data` mediumtext,
`userid` int(11) DEFAULT '0',
`username` varchar(150) DEFAULT '',
`usertype` varchar(50) DEFAULT '',
PRIMARY KEY (`session_id`),
KEY `whosonline` (`guest`,`usertype`),
KEY `userid` (`userid`),
KEY `time` (`time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

How to Solve Reindexing Issue for Large Catalog in Magento

I have approx 70000 products in my catalog.Whenever I do some changes on product level,it takes lot of time to update the change.
Check & follow these steps
To clear all the locks for re-indexing locate var/locks directory and remove all files under this directory.
Now, login to your MysQSL/phpMyAdmin to run the following MySQL query (Ensure that your have taken full backup before committing this MySQL query):
DELETE cpop.* FROM catalog_product_option_price AS cpop INNER
JOIN catalog_product_option AS cpo ON cpo.option_id =
cpop.option_id WHERE cpo.type = 'checkbox' OR cpo.type =
'radio' OR cpo.type = 'drop_down';
and
DELETE cpotp.* FROM catalog_product_option_type_price AS cpotp
INNER JOIN catalog_product_option_type_value AS cpotv ON
cpotv.option_type_id = cpotp.option_type_id INNER JOIN
catalog_product_option AS cpo ON cpotv.option_id = cpo.option_id
WHERE cpo.type <> 'checkbox' AND cpo.type <> 'radio' AND
cpo.type <> 'drop_down'
Log back in to your Magento Admin panel and go to System tab > Index Management hit index again and you will notice no such errors will appear again. You can follow these same steps again if re-indexing stops in future to resolve Magento ReIndexing issues.
Use three step in magento database (mysql) and Solve Re-indexing Problem
First step:
DROP TABLE IF EXISTS `index_process_event`;
DROP TABLE IF EXISTS `index_event`;
DROP TABLE IF EXISTS `index_process`;
Second step:
CREATE TABLE `index_event` (
`event_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`type` VARCHAR(64) NOT NULL,
`entity` VARCHAR(64) NOT NULL,
`entity_pk` BIGINT(20) DEFAULT NULL,
`created_at` DATETIME NOT NULL,
`old_data` MEDIUMTEXT,
`new_data` MEDIUMTEXT,
PRIMARY KEY (`event_id`),
UNIQUE KEY `IDX_UNIQUE_EVENT` (`type`,`entity`,`entity_pk`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `index_process` (
`process_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`indexer_code` VARCHAR(32) NOT NULL,
`status` ENUM('pending','working','require_reindex') NOT NULL DEFAULT 'pending',
`started_at` DATETIME DEFAULT NULL,
`ended_at` DATETIME DEFAULT NULL,
`mode` ENUM('real_time','manual') NOT NULL DEFAULT 'real_time',
PRIMARY KEY (`process_id`),
UNIQUE KEY `IDX_CODE` (`indexer_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
Third step:
INSERT INTO `index_process`(`process_id`,`indexer_code`,`status`,`started_at`,`ended_at`,`mode`) VALUES (1,'catalog_product_attribute','pending','2010-02-13 00:00:00','2010-02-13 00:00:00','real_time'),(2,'catalog_product_price','pending','2010-02-13 00:00:00','2010-02-13 00:00:00','real_time'),(3,'catalog_url','pending','2010-02-13 19:12:15','2010-02-13 19:12:15','real_time'),(4,'catalog_product_flat','pending','2010-02-13 00:00:00','2010-02-13 00:00:00','real_time'),(5,'catalog_category_flat','pending','2010-02-13 00:00:00','2010-02-13 00:00:00','real_time'),(6,'catalog_category_product','pending','2010-02-13 00:00:00','2010-02-13 00:00:00','real_time'),(7,'catalogsearch_fulltext','pending','2010-02-13 00:00:00','2010-02-13 00:00:00','real_time'),(8,'cataloginventory_stock','pending','2010-02-13 00:00:00','2010-02-13 00:00:00','real_time');
CREATE TABLE `index_process_event` (
`process_id` INT(10) UNSIGNED NOT NULL,
`event_id` BIGINT(20) UNSIGNED NOT NULL,
`status` ENUM('new','working','done','error') NOT NULL DEFAULT 'new',
PRIMARY KEY (`process_id`,`event_id`),
KEY `FK_INDEX_EVNT_PROCESS` (`event_id`),
CONSTRAINT `FK_INDEX_EVNT_PROCESS` FOREIGN KEY (`event_id`) REFERENCES `index_event` (`event_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_INDEX_PROCESS_EVENT` FOREIGN KEY (`process_id`) REFERENCES `index_process` (`process_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=INNODB DEFAULT CHARSET=utf8;

Resources