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
Related
How do I write this constrain in a Laravel migration?
ALTER TABLE user_profiles
ADD CONSTRAINT app_profiles
FOREIGN KEY (profile_id, app_id)
REFERENCES profile_apps (profile_id, app_id);
I've tried this:
$table->foreign(['profile_id', 'app_id'])->references('profile_apps')->on(['profile_id', 'app_id']);
which turns into:
"ErrorException : Array to string conversion" in
C:\Users\CAM\Projects\mcr-back\vendor\laravel\framework\src\Illuminate\Database\Grammar.php:39
and the same as strings,
$table->foreign('profile_id, app_id')->references('profile_apps')->on('profile_id, app_id');
with error:
"SQLSTATE[42000]: Syntax error or access violation: 1072 Key column
'profile_id, app_id' doesn't exist in table"
You are mix up on and references methods using.
Right variant is:
$table
->foreign(['profile_id', 'app_id'])
->references(['profile_id', 'app_id'])
->on('profile_apps');
Use this methoid
NOTE: Make sure in your database > migration the profile table and app table is above this table migration
2014_06_14_23123123_create_profiles_table
2014_06_14_23123123_create_apps_table
before
2014_06_14_23123123_create_this_table
$table->foreign('profile_id')->references('id')->on('profiles')->onDelete('cascade');
$table->foreign('app_id')->references('id')->on('apps')->onDelete('cascade');
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/
Here's my migration schema:
public function up()
{
Schema::create('objects', function (Blueprint $table) {
$table->increments('id');
$table->timestamp('timestamp1');
$table->timestamp('timestamp2');
});
}
But when I execute php artisan migrate, I get this error:
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'timestamp2' (SQL: create table objects (id int unsigned not null auto_increment primary key, timestamp1 timestamp not null, timestamp2 timestamp not null) default character set utf8mb4 collate utf8mb4_unicode_ci)
I must indicate that when I remove one of the 2 $table->timestamp(...); lines it works, but it doesn't when there is both. And the Object.php model is empty as it can be. Did I make a mistake?
I have read this post, but even though there is no longer errors when I change timestamp(...) into dateTime(...), I only want timestamps.
Timestamps are a little special, they must either be nullable or they must have a default value. So you must choose between timestamp('timestamp1')->nullable(); or timestamp('timestamp1')->useCurrent() or a custom default value like timestamp('timestamp1')->default(DB::raw('2018-01-01 15:23')).
I found this solution on laracasts:
nullableTimestamps() are only for default fields created_at, updated_at. for custom fields use timestamp()->nullable();
You can make one of the two timestamps nullable by using
timestamp()->nullable();
using your example, you would use:
$table->timestamp('timestamp2')->nullable();
Also laravel has built in timestamps by using
$table->timestamps();
which would automatically handle updated_at and created_at timestamping for you
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
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)