Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema Builder Issue 2nd Table Creation Issue #4497

Closed
nolros opened this issue May 27, 2014 · 3 comments
Closed

Schema Builder Issue 2nd Table Creation Issue #4497

nolros opened this issue May 27, 2014 · 3 comments

Comments

@nolros
Copy link

nolros commented May 27, 2014

I've been playing around Schema Builder and for some reason it will not longer create the 2nd table and it will not create composite key on the second table. I can run plain SQL (test SQL below) and get it done so unsure.

  1. Running latest version of Laravel.
  2. PHP 5.5
  3. mySQL 5.5.34-cll-lve
  4. Apache 2.2.25

if(DB::connection()->getDatabaseName())
{
echo "connected sucessfully to database ".DB::connection()->getDatabaseName();
}

Schema::create('users', function($usertable)
{

$usertable->bigIncrements('userid');    
$usertable->char('username',100);
$usertable->char('passwordhash',64);    
$usertable->char('name',100)->nullable();       
$usertable->char('firstname',100)->nullable();                      
$usertable->char('lastname',100)->nullable();       
$usertable->smallInteger('namevisible');    
$usertable->char('email',100);
$usertable->smallInteger('emailvisible');   
$usertable->smallInteger('userenabled');        
$usertable->dateTime('creationdate');   
$usertable->dateTime('modificatonDate');        
$usertable->dateTime('lastloggedin');   
$usertable->dateTime('lastprofileupdate');              
$usertable->smallInteger('visible');    
$usertable->smallInteger('status'); 
$usertable->primary('userid')->unique();                    

});
Schema::create('usergroups', function($usrgrptable)
{
$usrgrptable->bigIncrements('groupid');
$usrgrptable->bigInteger('userid', 20);
$usrgrptable->bigInteger('administrator', 20);
$usrgrptable->primary(array('groupid','userid','adminstrator'));
});

TEST SQL THAT WORKS FINE

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

USE matrixme_matrixme ;


-- Table matrixme_matrixme.users


CREATE TABLE IF NOT EXISTS matrixme_matrixme.users (
userid INT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(100) NULL,
firstname VARCHAR(100) NULL,
lastname VARCHAR(100) NULL,
PRIMARY KEY (userid))
ENGINE = InnoDB;


-- Table matrixme_matrixme.user groups


CREATE TABLE IF NOT EXISTS matrixme_matrixme.user groups (
groupid INT UNSIGNED NOT NULL AUTO_INCREMENT,
userid INT UNSIGNED NOT NULL,
admininstrator INT UNSIGNED NOT NULL,
PRIMARY KEY (groupid, userid, admininstrator),
INDEX fk_user_id_idx (userid ASC),
CONSTRAINT fk_user_id
FOREIGN KEY (userid)
REFERENCES matrixme_matrixme.users (userid)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

ERROR IN LOG FILE (weird as it was working beforehand and just stopped working)

[2014-05-26 20:13:53] production.ERROR: exception 'Illuminate\Database\QueryException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table users add primary key users_userid_primary(userid))' in /home/matrixme/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:555
Stack trace:
#0 /home/matrixme/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php(338): Illuminate\Database\Connection->run('alter table us...', Array, Object(Closure)) #1 /home/matrixme/public_html/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php(63): Illuminate\Database\Connection->statement('alter table us...')
#2 /home/matrixme/public_html/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php(169): Illuminate\Database\Schema\Blueprint->build(Object(Illuminate\Database\MySqlConnection), Object(Illuminate\Database\Schema\Grammars\MySqlGrammar))
#3 /home/matrixme/public_html/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php(112): Illuminate\Database\Schema\Builder->build(Object(Illuminate\Database\Schema\Blueprint))
#4 /home/matrixme/public_html/bootstrap/compiled.php(3165): Illuminate\Database\Schema\Builder->create('users', Object(Closure))
#5 /home/matrixme/public_html/app/routes.php(42): Illuminate\Support\Facades\Facade::__callStatic('create', Array)
#6 /home/matrixme/public_html/app/routes.php(42): Illuminate\Support\Facades\Schema::create('users', Object(Closure))
#7 [internal function]: {closure}()
#8 /home/matrixme/public_html/bootstrap/compiled.php(4954): call_user_func_array(Object(Closure), Array)
#9 /home/matrixme/public_html/bootstrap/compiled.php(4669): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#10 /home/matrixme/public_html/bootstrap/compiled.php(4657): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#11 /home/matrixme/public_html/bootstrap/compiled.php(706): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#12 /home/matrixme/public_html/bootstrap/compiled.php(687): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#13 /home/matrixme/public_html/bootstrap/compiled.php(1144): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true)
#14 /home/matrixme/public_html/bootstrap/compiled.php(7264): Illuminate\Http\FrameGuard->handle(Object(Illuminate\Http\Request), 1, true)
#15 /home/matrixme/public_html/bootstrap/compiled.php(7861): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true)
#16 /home/matrixme/public_html/bootstrap/compiled.php(7808): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true)
#17 /home/matrixme/public_html/bootstrap/compiled.php(10820): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true)
#18 /home/matrixme/public_html/bootstrap/compiled.php(648): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request))
#19 /home/matrixme/public_html/public/index.php(49): Illuminate\Foundation\Application->run()
#20 {main} [] []

@jarredholman
Copy link

I'm pretty sure the increments and bigIncrements types automatically make themselves primary keys, so explicitly declaring that column as the primary key later will cause a conflict.

It does seem like a bug if you can only create autoincrement/serial columns if they are the primary key.

@nolros
Copy link
Author

nolros commented May 28, 2014

I've tried it with both int,x and int types, still the same problem. Weird thing it was work fine then went South as I started working with composite keys. Even if I remove the primary key and add one field in the 2nd table I still have the same issue. SQL script, not problem, Schema builder ... nope. I've been running migrate on a number of apps lately without problems so I figure it is a user error :)

@taylorotwell
Copy link
Member

I can't really read this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants