I created a migration:
public function up()
{
Schema::create('accounts', function(Blueprint $table) {
$table->increments('id')->primary()->nullable(false)->index();
$table->string('username', 32)->nullable(false)->index();
$table->string('sha_pass_hash', 40)->nullable(false);
$table->string('sessionkey', 80)->nullable(false);
$table->string('v', 64)->nullable(false);
$table->string('s', 64)->nullable(false);
$table->string('token_key', 100)->nullable(false);
$table->string('email', 255)->nullable(false)->unique();
$table->timestamp('joindate')->nullable(false)->useCurrent();
$table->string('last_ip', 15)->nullable(false)->default('127.0.0.1');
$table->string('last_attempt_ip', 15)->nullable(false)->default('127.0.0.1');
$table->unsignedInteger('failed_logins', 10)->nullable(false)->default(0);
$table->unsignedTinyInteger('locked', 3)->nullable(false)->default(0);
$table->timestamp('last_login')->nullable(false)->default('0000-00-00 00:00:00');
$table->tinyInteger('online', 3)->nullable(false)->default(0);
$table->unsignedTinyInteger('expansion', 3)->nullable(false)->default(6);
$table->bigInteger('mutetime', 20)->nullable(false);
$table->string('mutereason', 255)->nullable(false);
$table->string('muteby', 50)->nullable(false);
$table->unsignedTinyInteger('locale', 50)->nullable(false);
$table->string('os', 3)->nullable(false);
$table->unsignedInteger('recruiter', 10)->nullable(false);
$table->unsignedInteger('battlenet_account', 10)->nullable(true)->default(NULL);
$table->tinyInteger('battlenet_index', 3)->nullable(true)->default(NULL);
});
}
And when i'm trying to executed command
php artisan migrate
I'm getting error:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'failed_logins' (SQL: create table `accounts` (`id` int unsigned
not null auto_increment primary key, `username` varchar(32) not null, `sha_pass_hash` varchar(40) not null, `sessionkey` varchar(80) not null, `v`
varchar(64) not null, `s` varchar(64) not null, `token_key` varchar(100) not null, `email` varchar(255) not null, `joindate` timestamp default CURR
ENT_TIMESTAMP not null, `last_ip` varchar(15) not null default '127.0.0.1', `last_attempt_ip` varchar(15) not null default '127.0.0.1', `failed_log
ins` int unsigned not null default '0' auto_increment primary key, `locked` tinyint unsigned not null default '0' auto_increment primary key, `last
_login` timestamp not null default '0000-00-00 00:00:00', `online` tinyint not null default '0' auto_increment primary key, `expansion` tinyint uns
igned not null default '6' auto_increment primary key, `mutetime` bigint not null auto_increment primary key, `mutereason` varchar(255) not null, `
muteby` varchar(50) not null, `locale` tinyint unsigned not null auto_increment primary key, `os` varchar(3) not null, `recruiter` int unsigned not
null auto_increment primary key, `battlenet_account` int unsigned null auto_increment primary key, `battlenet_index` tinyint null auto_increment p
rimary key) default character set utf8 collate utf8_unicode_ci)
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'failed_logins'
What i noticed is that every integer type gets auto_increment primary key in sql query. I think it could be the problem, as far as know that auto increment columns can't have default values.
If you take a look at the source code for the unsignedInteger method, you'll notice the second parameter doesn't specify length, but rather is for an autoIncrement boolean. You'll need to update your various integer methods accordingly.
Related
CREATE TABLE `chuchutvlogin`. (
`id` INT NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(20) NOT NULL ,
`gender` CHAR(1) NOT NULL ,
`email` VARCHAR(50) NOT NULL ,
`password` VARCHAR(20) NOT NULL ,
`number` BIGINT(10) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
what's issue?
i was creating a login databse and error #1064 came syntax error in localhost phpmyadmnin
Fix syntax error in your query, remove dot char after table name:
CREATE TABLE chuchutvlogin (
id INT NOT NULL AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
gender CHAR(1) NOT NULL,
email VARCHAR(50) NOT NULL,
password VARCHAR(20) NOT NULL,
number BIGINT(10) NOT NULL,
PRIMARY KEY (id)
) ENGINE = InnoDB;
dbfiddle
I have this symfony application to use as reference and learning tool, and it's not written by me. Anyway to use the application I need to create the database and the tables, but when try to do so with doctrine migrations it complains that there's some syntax errors, since I didn't write the SQL code I have no idea what's wrong because it stops only at users table but other tables are created succesfully.
Here's the error message:
An exception occurred while executing 'CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(180) NOT NULL, roles JSON NOT NULL, passw VARCHAR(255) NOT NU
LL, name VARCHAR(50) NOT NULL, is_blocked TINYINT(1) NOT NULL, UNIQUE INDEX UNIQ_8D93D649E7927C74 (email), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf
8mb4_unicode_ci ENGINE = InnoDB':
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 th
e right syntax to use near 'JSON NOT NULL, passw VARCHAR(255) NOT NULL, name VARCHAR(50) NOT NULL, is_blocke' at line 1
Here's the generated migration:
$this->addSql('CREATE TABLE country (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(30) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE game (id INT AUTO_INCREMENT NOT NULL, referee_id INT DEFAULT NULL, tournament_id INT NOT NULL, home_team_id INT NOT NULL, away_team_id INT NOT NULL, date DATETIME NOT NULL, round SMALLINT DEFAULT NULL, game_nr SMALLINT NOT NULL, is_playoffs_game TINYINT(1) NOT NULL, playoffs_round SMALLINT DEFAULT NULL, group_nr SMALLINT DEFAULT NULL, home_score SMALLINT DEFAULT NULL, away_score SMALLINT DEFAULT NULL, INDEX IDX_232B318C4A087CA2 (referee_id), INDEX IDX_232B318C33D1A3E7 (tournament_id), INDEX IDX_232B318C9C4C13F6 (home_team_id), INDEX IDX_232B318C45185D02 (away_team_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE player (id INT AUTO_INCREMENT NOT NULL, team_id INT NOT NULL, name VARCHAR(100) NOT NULL, goal_count INT NOT NULL, b_date DATETIME NOT NULL, number SMALLINT DEFAULT NULL, INDEX IDX_98197A65296CD8AE (team_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE team (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, country_id INT NOT NULL, tournament_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, venue VARCHAR(255) DEFAULT NULL, contacts VARCHAR(1000) NOT NULL, region VARCHAR(100) NOT NULL, state SMALLINT NOT NULL, INDEX IDX_C4E0A61FA76ED395 (user_id), INDEX IDX_C4E0A61FF92F3E70 (country_id), INDEX IDX_C4E0A61F33D1A3E7 (tournament_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE tournament (id INT AUTO_INCREMENT NOT NULL, country_id INT NOT NULL, type_id INT NOT NULL, user_id INT NOT NULL, tournament_desc VARCHAR(1000) DEFAULT NULL, rounds SMALLINT DEFAULT NULL, playoffs_places SMALLINT DEFAULT NULL, rounds_per_pair SMALLINT DEFAULT NULL, group_count SMALLINT DEFAULT NULL, players_on_field SMALLINT NOT NULL, rules VARCHAR(1000) DEFAULT NULL, prizes VARCHAR(255) DEFAULT NULL, region VARCHAR(255) NOT NULL, venue VARCHAR(255) DEFAULT NULL, contacts VARCHAR(255) DEFAULT NULL, code VARCHAR(9) NOT NULL, is_started TINYINT(1) NOT NULL, is_ended TINYINT(1) NOT NULL, name VARCHAR(255) NOT NULL, UNIQUE INDEX UNIQ_BD5FB8D977153098 (code), INDEX IDX_BD5FB8D9F92F3E70 (country_id), INDEX IDX_BD5FB8D9C54C8C93 (type_id), INDEX IDX_BD5FB8D9A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE tournament_type (id INT AUTO_INCREMENT NOT NULL, type VARCHAR(50) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(180) NOT NULL, roles JSON NOT NULL, passw VARCHAR(255) NOT NULL, name VARCHAR(50) NOT NULL, is_blocked TINYINT(1) NOT NULL, UNIQUE INDEX UNIQ_8D93D649E7927C74 (email), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('ALTER TABLE game ADD CONSTRAINT FK_232B318C4A087CA2 FOREIGN KEY (referee_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE game ADD CONSTRAINT FK_232B318C33D1A3E7 FOREIGN KEY (tournament_id) REFERENCES tournament (id)');
$this->addSql('ALTER TABLE game ADD CONSTRAINT FK_232B318C9C4C13F6 FOREIGN KEY (home_team_id) REFERENCES team (id)');
$this->addSql('ALTER TABLE game ADD CONSTRAINT FK_232B318C45185D02 FOREIGN KEY (away_team_id) REFERENCES team (id)');
$this->addSql('ALTER TABLE player ADD CONSTRAINT FK_98197A65296CD8AE FOREIGN KEY (team_id) REFERENCES team (id)');
$this->addSql('ALTER TABLE team ADD CONSTRAINT FK_C4E0A61FA76ED395 FOREIGN KEY (user_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE team ADD CONSTRAINT FK_C4E0A61FF92F3E70 FOREIGN KEY (country_id) REFERENCES country (id)');
$this->addSql('ALTER TABLE team ADD CONSTRAINT FK_C4E0A61F33D1A3E7 FOREIGN KEY (tournament_id) REFERENCES tournament (id)');
$this->addSql('ALTER TABLE tournament ADD CONSTRAINT FK_BD5FB8D9F92F3E70 FOREIGN KEY (country_id) REFERENCES country (id)');
$this->addSql('ALTER TABLE tournament ADD CONSTRAINT FK_BD5FB8D9C54C8C93 FOREIGN KEY (type_id) REFERENCES tournament_type (id)');
$this->addSql('ALTER TABLE tournament ADD CONSTRAINT FK_BD5FB8D9A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)');
I have a problem with my Database (using MariaDB) to insert a new row
I Having the following errors
Cannot add or update a child row: a foreign key constraint fails
(DbName_boutique.Commande, CONSTRAINT bdc_managed_by___fk FOREIGN
KEY (bdc_managed_by_user_id) REFERENCES utilisateur
(utilisateur_id))INSERT INTO Commande (lien_devis, lien_bdc,
end_devis, bdc_status, devis_created_by_user_id,
bdc_managed_by_user_id, entreprise_id)
I have this code on my controller :
$unknow = NULL;
$end_date = date('d/m/Y', strtotime("+1 month"));
$user = checkConnected($bdd);
Commande::create($bdd, $new_full_modele_devis_path, $new_full_modele_devis_path, $end_date, 0, $user->getId(), $user->getId(), $entreprise->getId());
The $unknow are values I used later in an another Controller.
My Table "Commande" is built like following :
create table dBName.Commande
(
commande_id int not null
primary key,
lien_devis varchar(255) null,
lien_bdc varchar(255) null,
end_devis datetime null,
bdc_status int null,
devis_created_by_user_id mediumint unsigned null,
bdc_managed_by_user_id mediumint unsigned null,
entreprise_id smallint(5) unsigned null,
constraint bdc_managed_by___fk
foreign key (bdc_managed_by_user_id) references dBName.utilisateur (utilisateur_id),
constraint devis_created_by___fk
foreign key (devis_created_by_user_id) references dBName.utilisateur (utilisateur_id),
constraint entreprise___fk
foreign key (entreprise_id) references dBName.entreprise (entreprise_id)
);
And I have later the user table :
create table dBName.utilisateur
(
utilisateur_id mediumint unsigned auto_increment
primary key,
utilisateur_password varchar(255) null,
utilisateur_nom varchar(50) null,
utilisateur_prenom varchar(50) null,
utilisateur_email varchar(255) null,
utilisateur_telephone varchar(10) null,
utilisateur_fax varchar(25) null,
is_active tinyint(1) null
)
charset = utf8mb4;
And entreprise :
create table dBName.entreprise
(
entreprise_id smallint(5) unsigned auto_increment
primary key,
entreprise_nom varchar(100) null,
entreprise_siret char(14) null,
entreprise_telephone char(10) null,
entreprise_salesforce_number varchar(100) null,
entreprise_compte_client varchar(100) null,
entreprise_raison_sociale varchar(100) null,
entreprise_APE varchar(25) null,
entreprise_image_link varchar(255) null,
adresse_id mediumint unsigned null,
constraint FK_entreprise_adresse_id
foreign key (adresse_id) references dBName.adresse (adresse_id)
)
charset = utf8mb4;
I don't understand Why i'm Habing the error... And bit of help could help me a lot.. Thank you.
In Commande table, the column bdc_managed_by_user_id is mapped to utilisateur_id column of utilisateur table via foreign key reference.
This means, whenever you insert a row in Commande table, corresponding bdc_managed_by_user_id has to be one of the utilisateur_id present in utilisateur table. If not, you will get the above error.
This is called Foreign Key Constraint and you can have a look at this example describing how it works.
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table def
inition; there can be only one auto column and it must be defined as a key
(SQL: create table `students` (`id` int unsigned not null auto_increment pr
imary key, `student_id` int unsigned not null auto_increment primary key, `
stuent_name` varchar(50) not null, `student_email` varchar(50) not null, `s
tudent_username` varchar(50) not null, `student_phone` varchar(50) not null
, `student_photo` text not null, `student_sex` varchar(191) not null, `stud
ent_status` varchar(191) not null, `created_at` timestamp null, `updated_at
` timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)
i dont know what happen , this problem is happen when i try to migrate
You have two auto_increment key id and studuent_id. You can only set one as the auto increment key.
I am getting the following error:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous
And I have no idea where is it coming from. The stacktrace is not clear about it either. The query that throws this error:
Database::executeQuery('CREATE TEMPORARY TABLE tmp_inventory ENGINE=MEMORY '
. 'SELECT id, email_hash, mailing_list_id, ttl, price, last_click, last_view, extra_data '
. 'FROM inventory i INNER JOIN mailing_list ml on i.mailing_list_id = ml.id '
. 'WHERE i.active = 0 AND i.deleted = 1 AND i.completely_deleted = 1 AND i.resting_to < NOW() AND i.next_sync_at < NOW() AND ml.active = 0 '
. 'LIMIT 10;');
So I have two tables - inventory, and mailing_list. Inventory has the following structure:
CREATE TABLE `inventory` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email_hash` char(32) NOT NULL,
`mailing_list_id` int(6) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`last_send_at` datetime DEFAULT NULL,
`resting_to` datetime DEFAULT NULL,
`next_sync_at` datetime DEFAULT NULL,
`ttl` datetime DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`deleted` tinyint(1) NOT NULL DEFAULT '0',
`completely_deleted` tinyint(1) NOT NULL DEFAULT '0',
`price` int(10) unsigned NOT NULL,
`last_view` datetime DEFAULT NULL,
`last_view_at` datetime DEFAULT NULL,
`last_updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
)
And the mailing_list:
CREATE TABLE `mailing_list` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '0',
`created_at` datetime NOT NULL,
`price` int(10) unsigned NOT NULL DEFAULT '1000',
`ttl` int(10) unsigned NOT NULL DEFAULT '604800',
`resting_time` int(10) unsigned NOT NULL DEFAULT '0',
`email_from` varchar(255) DEFAULT NULL,
`email_return_path` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)
Whats wrong?
In your query you are accessing 2 tables: inventory in your FROM clause, mailing_list in your INNER JOIN clause.
Both of these tables have a column named id, so your db doesn't know which column you are referring to.
To fix this, specify the table in your select, by replacing id with i.id or ml.id