Skip to content

Instantly share code, notes, and snippets.

@milinmestry
Created March 20, 2018 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milinmestry/9bb930b59cf36a5e1a7e17e3ac28d4c2 to your computer and use it in GitHub Desktop.
Save milinmestry/9bb930b59cf36a5e1a7e17e3ac28d4c2 to your computer and use it in GitHub Desktop.
laravel 5.6 Illuminate\Database\QueryException : SQLSTATE[42000]
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePropertiesTable extends Migration {
const TABLE_NAME = 'properties';
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create(self::TABLE_NAME, function (Blueprint $table) {
$table->increments('id');
$table->integer('seller_id'); // users table FK
$table->char('property_type', 50);
$table->char('apartment_type', 25);
$table->char('measurement', 10);
$table->char('measurement_type', 20);
$table->char('maintenance_charges', 10)->nullable();
$table->char('ownership', 20); // Individual, Joint
$table->char('joint_owners_name', 100)->nullable();
$table->integer('sale_price', 10); // <---------------------------- This line creates problem
$table->integer('min_expected_price', 10)->nullable(); // <---------------------------- This line creates problem
$table->string('address', 500);
$table->string('homeloan_details', 500)->nullable();
$table->string('amenities', 800)->nullable();
$table->text('locality_features')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists(self::TABLE_NAME);
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePropertiesTable extends Migration {
const TABLE_NAME = 'properties';
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create(self::TABLE_NAME, function (Blueprint $table) {
$table->increments('id');
$table->integer('seller_id'); // users table FK
$table->char('property_type', 50);
$table->char('apartment_type', 25);
$table->char('measurement', 10);
$table->char('measurement_type', 20);
$table->char('maintenance_charges', 10)->nullable();
$table->char('ownership', 20); // Individual, Joint
$table->char('joint_owners_name', 100)->nullable();
$table->integer('sale_price'); // <---------------------------- problem fixed
$table->integer('min_expected_price')->nullable(); // <---------------------------- problem fixed
$table->string('address', 500);
$table->string('homeloan_details', 500)->nullable();
$table->string('amenities', 800)->nullable();
$table->text('locality_features')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists(self::TABLE_NAME);
}
}
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; t
here can be only one auto column and it must be defined as a key
(SQL: create table `properties` (`id` int unsigned not null auto_increment primary key, `seller_id` int not null, `
property_type` char(50) not null, `apartment_type` char(25) not null, `measurement` char(10) not null,
`measurement_type` char(20) not null, `maintenance_charges` char(10) null, `ownership` char(20) not null,
`joint_owners_name` char(100) null, `sale_price` int not null auto_increment primary key,
`min_expected_price` int null auto_increment primary key, `address` varchar(500) not null,
`homeloan_details` varchar(500) null, `amenities` varchar(800) null, `locality_features` text null,
`created_at` timestampnull, `updated_at` timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)
at /var/www/html/sale-my-house/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|
@mfrancaleal
Copy link

mfrancaleal commented May 10, 2019

I suggest to use the type decimal() when it's price: $table->decimal('price', 10,2).

Case want to integer, change integer() to smallInteger().
$table->smallInteger('sale_price');
$table->smallInteger('min_expected_price')->nullable();

See https://laravel.com/docs/5.8/migrations#columns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment