1) I created User Class with make:user command:
Console image
2) I generated migration file with make:migration command.
3) Here is the up() function in migration file:
final class Version20181110133851 extends AbstractMigration
{
public function up(Schema $schema) : void
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() !==
'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE user ADD email VARCHAR(180) NOT NULL, ADD roles JSON NOT NULL, CHANGE id id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)');
$this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649E7927C74 ON user (email)');
}
...
}
4) This is what happens when I try to migrate to data base with doctrine:migration:migrate command
Image showing the error
and here is the content of the error:
-> ALTER TABLE user ADD email VARCHAR(180) NOT NULL, ADD roles JSON NOT
NULL, CHANGE id id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)
Migration 20181110133851 failed during Execution. Error An exception
occurred while executing 'ALTER TABLE user ADD email VARCHAR(180) NOT
NULL, ADD roles JSON NOT NULL, CHANGE id id INT AUTO_INCREMENT NOT NULL,
ADD PRIMARY KEY (id)':
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error
in your SQL syntax; check the manual that corresponds to your MariaDB
server version for the right syntax to use near 'JSON NOT NULL, CHANGE id
id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)' at line 1
In AbstractMySQLDriver.php line 99:
An exception occurred while executing 'ALTER TABLE user ADD email
VARCHAR(180) NOT NULL, ADD roles JSON NOT NULL, CHANGE id id INT
AUTO_INCREMENT NOT NULL, ADD PRI
MARY KEY (id)':
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error
in your SQL syntax; check the manual that corresponds to your MariaDB
server version for
the right syntax to use near 'JSON NOT NULL, CHANGE id id INT
AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)' at line 1
In PDOConnection.php line 109:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error
in your SQL syntax; check the manual that corresponds to your MariaDB
server version for
the right syntax to use near 'JSON NOT NULL, CHANGE id id INT
AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)' at line 1
In PDOConnection.php line 107:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error
in your SQL syntax; check the manual that corresponds to your MariaDB
server version for
the right syntax to use near 'JSON NOT NULL, CHANGE id id INT
AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)' at line 1
5) I've been trying to solve it for hours, but I am still a newbie and I don't think I am going to solve it myself.
6) My question: Where is the SYNTAX ERROR?
As #Padam87 said, MariaDB does not support the native JSON type. If you can't upgrade your database for whatever reason but don't want to change the column type, just define the database version in the Doctrine properties and let Doctrine handle the conversion for you.
doctrine.yaml:
doctrine:
dbal:
server_version: '10.1.36-MariaDB'
This will add a comment in the table definition which Doctrine will use later to encode/decode the values.
Mariadb 10.1 does not support the native json type, so you will have to use another type. Change your column definition, delete this migration, and generate a new one.
Alternatively, you can upgrade. Mariadb starts supporting the json type from 10.2.7 and up. https://mariadb.com/kb/en/library/json-data-type/
Related
I install laravel 5.5 and When I run php artisan migrate show me this error
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQ
L: alter table users add unique users_email_unique(email))
And I add below code on AppServiceProvider.php
public function boot()
{
Schema::defaultStringLength(191); //Solved by increasing StringLength
}
And then show me this error
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'created_at' (SQL: create table
password_resets (email varchar(191) not null, token varchar(191) not null, created_at timestamp not null) de
fault character set utf8mb4 collate utf8mb4_unicode_ci)
You can use nullableTimestamps() instead of timestamps()
or
$table->timestamp('created_at')->default(\DB::raw('CURRENT_TIMESTAMP'));
also check the database server version
Please have a look on these ref links:
https://github.com/laravel/framework/issues/3602
https://laracasts.com/discuss/channels/forge/syntax-error-or-access-violation-1067-invalid-default-value-for-created-at
I'm currently using laravel 7.
I just go to config/database.php
Then I change strict => true to strict => false.
Then run the same migration again. It works.
my case
$table->timestamp('deleted_at')->nullable()->default(null);
this worked
I'm trying to create a json database in XAMP, while using the phpmyAdmin it showed me that I'm using mariaDB but in my xamp-control panel v3.2.2 it shows running mySQL on port 3306. I'm using Laravel 5.4 framework to create the database, following is my migration which I'm trying to execute:
Schema::connection('newPortal')->create('pages', function (Blueprint $table){
$table->increments('id');
$table->string('title');
$table->string('slug')->unique()->index();
$table->json('styles')->nullable();
$table->json('content')->nullable();
$table->json('scripts')->nullable();
$table->softDeletes();
$table->timestamps();
});
Now while executing this I'm getting following error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'json null, content json null, scripts json null, deleted_at timestamp null' at line 1 (SQL: create table pages (id int unsigned not null auto_increment primary key, title varchar(191) not null, slug varchar(191) not null, styles json null, content json null, scripts json null, deleted_at timestamp null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)
Even if I keep not null it throws the same error. I want to have json formatted data, I checked the supported version and as per the documentation json format support started from the version MariaDB 10.0.16. and I'm using 10.1.21-MariaDB
Help me out in this.
Since MariaDB version 10.2.7; theJSON data-type is an alias for LONGTEXT.
If you are having issues with the JSON data-type in MariaDB, simply just change it to LONGTEXT. ;-)
Or add MariaDB JSON to Laravel with this package
Note that the 1064 complained about the datatype "json". Such is not (yet) implemented in MariaDB.
You can get close with Dynamic Columns, which at least has a way of fetching them into JSON syntax.
Another thing (probably what you are referring to) is CONNECT being able to have a JSON table type. (Not column type.)
MySQL 5.7 has a datatype called JSON, plus a bunch of functions to manipulate such.
Figured out a simple workaround (not recommended for production) -
As per mariadb version 10.1.32 and lower it seems like mariadb does not support json data type I am still unsure if it is available in version 10.2.7+.
but here's a simple workaround to get through this.
change json data type into text and then run your migration again.
(https://user-images.githubusercontent.com/27993070/41234555-19c5d1d8-6dbf-11e8-9a4b-0644b03aecfc.png)
source-
https://github.com/laravel/framework/issues/13622
Add MariaDB JSON support to Laravel by running this command using composer:
composer require ybr-nx/laravel-mariadb
If you are using Larvel 5.3 and 5.4 do these two items:
Include MariaDBServiceProvider in config/app.php by adding this
line to providers:
'providers' => [
// other exist providers
YbrNX\MariaDB\MariaDBServiceProvider::class,
]
Set default connection in database configuration to mariadb:
'defaultconnection' => [
'driver' => 'mariadb',
Adding package is done and then you can use functionalities.
In Migrations:
$table->json('field') //CHECK (JSON_VALID(field))
$table->json('field')->nullable() //CHECK (field IS NULL OR JSON_VALID(field))
For Query builder:
$query->where('somejson->something->somethingelse', 2)
DB::table('sometable')->select('sometable.somedata', 'sometable.somejson->somedata as somejsondata')
Also, JSON_SET() works in MariaDB as in MySQL 5.7:
DB::table('sometable')->where('somejson->somedata', $id)->update(['somejson->otherdata' => 'newvalue']);
Note 1: MariaDB has an alias for JSON datatype since version 10.2.7
Note 2: There is bug in MariaDB < 10.2.8 JSON_EXTRACT() behaviour
function. It's fixed in MariaDB 10.2.8
retrieved from
public function up()
{
Schema::create('jadwal_praks', function (Blueprint $table) {
$table->increments('id');
$table->integer('thnajrn_id', 10)->unsigned();
$table->foreign('thnajrn_id')->references('id')->on('tahun_ajarans');
$table->integer('prak_id', 10)->unsigned();
$table->foreign('prak_id')->references('Prak_kode')->on('mata_praks');
$table->integer('hari_id', 10)->unsigned();
$table->foreign('hari_id')->references('id')->on('haris');
$table->integer('jam_id', 10)->unsigned();
$table->foreign('jam_id')->references('id')->on('jams');
$table->integer('ruang_id', 10)->unsigned();
$table->foreign('ruang_id')->references('id')->on('ruangs');
$table->integer('kap_id', 10)->unsigned();
$table->foreign('kap_id')->references('id')->on('kapasitas');
$table->timestamps();
$table->rememberToken();
});
}
After run php artisan migrate
[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error
or access violation: 1075 Incorrect table definition; there can be
only one auto co lumn and it must be defined as a key (SQL: create
table jadwal_praks (id int unsigned not null auto_increment
primary key, thnajrn_id int unsigned not null auto_increment primary
key, prak_id int unsigned not null auto _increment primary key,
hari_id int unsigned not null auto_increment primary key, jam_id
int unsigned not nul l auto_increment primary key, ruang_id int
unsigned not null auto_increment primary key, kap_id int unsigned
not null auto_increment primary key, created_at timestamp null,
updated_at timestamp null, remember_token v archar(100) null)
default character set utf8 collate utf8_unicode_ci)
And this
[PDOException] SQLSTATE[42000]: Syntax error or access violation:
1075 Incorrect table definition; there can be only one auto column and
it must be defined as a key
With my observation, I would say you should remove default value you added to the your foreign fields from (for example):
$table->integer('thnajrn_id', 10)->unsigned();
To:
$table->integer('thnajrn_id')->unsigned();
PS: With a similar experience, I know presently this works with one of the projects I work with in Laravel 5.2.*
Hope this helps :)
When you use $table->integer('thnajrn_id', 10)->unsigned(); this means your setting the second parameter as true which represents AutoIncrement
try
table->integer('thnajrn_id')->length(10)->unsigned();
More info
When you use $table->unsignedBigInteger('account_id')->nullable(); you can still get this same error, since nullable() produces a default value can produce that same error. So always leave your foreign key empty, neither nullable or default(0)
I am trying to create a table with this code.
public function up()
{
Schema::create('BookInfo', function (Blueprint $table) {
$table->increments('id');
$table->integer('bookId',11);
$table->string('Name',255);
$table->string('Publisher')->nullable();
$table->integer('Publishing_year',4)->nullable();
$table->integer('Price',5)->nullable();
$table->string('Language',30);
$table->timestamps();
});
}
When I tried php artisan migrate it shows me this error.
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect
table definition; there can be only one auto column and it must
be defined as a key (SQL: create tableBookInfo(bookIdint not
null auto_increment primary key,Namevarchar(255) not null,
Publishervarchar(255) null,Publishing_yearint null
auto_increment primary key,Priceint null auto_increment primary key,
Languagevarchar(30) not null,created_attimestamp default 0
not null,updated_attimestamp default 0 not null) default character
set utf8 collate utf8_unicode_ci) and
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect
table definition; there can be only one auto column and it must be
defined as a key
It seems laravel takes all the integer columns as auto-increment.What happened here actually and what will be solution?
The error says it all,
$table->integer('bookId',11);
^// this is considered as autoincrement rather than length
There is no length option for integers
See this
References on SO, this and this
$table->integer('bookId',11); is not syntactically correct in Eloquent ORM(you can't set any size limit for integers) and that is causing the error in your case.
And $table->increments('id'); automatically sets id as a primary key.
Find all necessary table building commands in Eloquent ORM here: http://laravel.com/docs/4.2/schema#adding-columns
At the first time I got such error, I realize the problem was similar to this question > Error #1064 in mysql So I changed the TYPE=MyISAM on my code to ENGINE=MyISAM.
But even after the changing, I encountered similar error. It says,
mySQL error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(14),
PRIMARY KEY (id)
) ENGINE=M' at line 7.
I thought changing to ENGINE=MyISAM would fix the error, but it doesn't. What should I do?
Here is my code:
$queries[] = "CREATE TABLE IF NOT EXISTS {$prefix}conversationlog (
id int(11) NOT NULL auto_increment,
input text default '',
response text default '',
thatresponse text default '',
uid varchar(255) default NULL,
enteredtime timestamp(14),
PRIMARY KEY (id)
) ENGINE=MyISAM";
The error is at the (14), not ENGINE=MyISAM. The TIMESTAMP type does not take a size; it's always timestamp-sized.