SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

Does a multiclassed wizard start with a spellbook?

Minimizing medical costs with HSA

Boss has banned cycling to work because he thinks it's unsafe

Is there a typical layout to blocking installed for backing in new construction framing?

In the Seventh Seal why does Death let the chess game happen?

Why has Novell never written its own boot loader?

Should I warn my boss I might take sick leave

Why is the saxophone not common in classical repertoire?

Is it possible to spoof an IP address to an exact number?

Motorcyle Chain needs to be cleaned every time you lube it?

Bypass with wrong cvv of debit card and getting OTP

Do we have a much compact and generalized version of erase–remove idiom?

how can i make this execution plan more efficient?

Why did moving the mouse cursor cause Windows 95 to run more quickly?

What's the big deal about the Nazgûl losing their horses?

Has chattel slavery ever been used as a criminal punishment in the USA since the passage of the Thirteenth Amendment?

Show that there are infinitely more problems than we will ever be able to compute

Explain how 'Sharing the burden' puzzle from Professor Layton and the Miracle Mask should be solved

PhD: When to quit and move on?

Do intermediate subdomains need to exist?

Who pays for increased security measures on flights to the US?

Can mxd files be under version control?

How to supply water to a coastal desert town with no rain and no freshwater aquifers?

What do you call the angle of the direction of an airplane?



SQL error code 1064 with creating Laravel foreign keys


Foreign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















I have a table bankaccounts. In there I have a column 'user_id' that refers to the id of in the user table.



Now I want to add a foreign key to that column, but then I get this error.




>
SQLSTATE[42000]: Syntax error or access violation: 1064 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 'unsigned not null, created_at timestamp null, updated_at timestamp null) def' at line 1 (SQL: create table bankaccounts (id bigint unsigned not null auto_increment primary key, user_id int unsigned not null, accountnumber varchar(191) not null, bank varchar(191) not null, user_name varchar(191) unsigned not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')




I've already tried it with just the minimum change. Only add a foreign key on the user_id column.



I have the standard Laravel users migration and this is the bankaccounts migration.



public function up()

Schema::create('bankaccounts', function (Blueprint $table)
$table->bigIncrements('id');
$table->integer('user_id')->unsigned();
$table->string('accountnumber')->unique();
$table->string('bank');
$table->string('user_name');
$table->timestamps();
);

Schema::table('bankaccounts', function($table)
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('user_name')->references('name')->on('users')->onDelete('cascade');
);



I have no idea what I'm doing wrong, because I see tutorials online that also try it with the id from the users table, and they succeed.










share|improve this question






























    -1















    I have a table bankaccounts. In there I have a column 'user_id' that refers to the id of in the user table.



    Now I want to add a foreign key to that column, but then I get this error.




    >
    SQLSTATE[42000]: Syntax error or access violation: 1064 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 'unsigned not null, created_at timestamp null, updated_at timestamp null) def' at line 1 (SQL: create table bankaccounts (id bigint unsigned not null auto_increment primary key, user_id int unsigned not null, accountnumber varchar(191) not null, bank varchar(191) not null, user_name varchar(191) unsigned not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')




    I've already tried it with just the minimum change. Only add a foreign key on the user_id column.



    I have the standard Laravel users migration and this is the bankaccounts migration.



    public function up()

    Schema::create('bankaccounts', function (Blueprint $table)
    $table->bigIncrements('id');
    $table->integer('user_id')->unsigned();
    $table->string('accountnumber')->unique();
    $table->string('bank');
    $table->string('user_name');
    $table->timestamps();
    );

    Schema::table('bankaccounts', function($table)
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->foreign('user_name')->references('name')->on('users')->onDelete('cascade');
    );



    I have no idea what I'm doing wrong, because I see tutorials online that also try it with the id from the users table, and they succeed.










    share|improve this question


























      -1












      -1








      -1








      I have a table bankaccounts. In there I have a column 'user_id' that refers to the id of in the user table.



      Now I want to add a foreign key to that column, but then I get this error.




      >
      SQLSTATE[42000]: Syntax error or access violation: 1064 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 'unsigned not null, created_at timestamp null, updated_at timestamp null) def' at line 1 (SQL: create table bankaccounts (id bigint unsigned not null auto_increment primary key, user_id int unsigned not null, accountnumber varchar(191) not null, bank varchar(191) not null, user_name varchar(191) unsigned not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')




      I've already tried it with just the minimum change. Only add a foreign key on the user_id column.



      I have the standard Laravel users migration and this is the bankaccounts migration.



      public function up()

      Schema::create('bankaccounts', function (Blueprint $table)
      $table->bigIncrements('id');
      $table->integer('user_id')->unsigned();
      $table->string('accountnumber')->unique();
      $table->string('bank');
      $table->string('user_name');
      $table->timestamps();
      );

      Schema::table('bankaccounts', function($table)
      $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
      $table->foreign('user_name')->references('name')->on('users')->onDelete('cascade');
      );



      I have no idea what I'm doing wrong, because I see tutorials online that also try it with the id from the users table, and they succeed.










      share|improve this question
















      I have a table bankaccounts. In there I have a column 'user_id' that refers to the id of in the user table.



      Now I want to add a foreign key to that column, but then I get this error.




      >
      SQLSTATE[42000]: Syntax error or access violation: 1064 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 'unsigned not null, created_at timestamp null, updated_at timestamp null) def' at line 1 (SQL: create table bankaccounts (id bigint unsigned not null auto_increment primary key, user_id int unsigned not null, accountnumber varchar(191) not null, bank varchar(191) not null, user_name varchar(191) unsigned not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')




      I've already tried it with just the minimum change. Only add a foreign key on the user_id column.



      I have the standard Laravel users migration and this is the bankaccounts migration.



      public function up()

      Schema::create('bankaccounts', function (Blueprint $table)
      $table->bigIncrements('id');
      $table->integer('user_id')->unsigned();
      $table->string('accountnumber')->unique();
      $table->string('bank');
      $table->string('user_name');
      $table->timestamps();
      );

      Schema::table('bankaccounts', function($table)
      $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
      $table->foreign('user_name')->references('name')->on('users')->onDelete('cascade');
      );



      I have no idea what I'm doing wrong, because I see tutorials online that also try it with the id from the users table, and they succeed.







      php sql laravel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 19:25







      idontunderstandarrays

















      asked Mar 25 at 19:11









      idontunderstandarraysidontunderstandarrays

      8910 bronze badges




      8910 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          3














          You can't have a "unsigned" varchar.



          You have



          user_name varchar(191) unsigned not null


          change it to



          user_name varchar(191) not null





          share|improve this answer


















          • 2





            In the laravel migration, this is caused by $table->string('user_name')->unsigned();

            – Devon
            Mar 25 at 19:16






          • 1





            Technically, because this is Laravel, OP needs to change $table->string('user_name')->unsigned(); to $table->string('user_name');.

            – Ed Cottrell♦
            Mar 25 at 19:16












          • I edited my post. Even without the name, it still won't work. Now I'm getting this error: General error: 1215 Cannot add foreign key constraint (SQL: alter table bankaccounts add constraint bankaccounts_user_id_foreign foreign key (user_id) references users (id) on delete cascade)

            – idontunderstandarrays
            Mar 25 at 19:17






          • 1





            @idontunderstandarrays That's a different problem and should be asked in a different question.

            – Ed Cottrell♦
            Mar 25 at 19:17











          • @EdCottrell Okay well, I changed it to your suggestion, removed the foreign key for the user_id and I'm also getting that same error.

            – idontunderstandarrays
            Mar 25 at 19:19










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55344918%2fsql-error-code-1064-with-creating-laravel-foreign-keys%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          You can't have a "unsigned" varchar.



          You have



          user_name varchar(191) unsigned not null


          change it to



          user_name varchar(191) not null





          share|improve this answer


















          • 2





            In the laravel migration, this is caused by $table->string('user_name')->unsigned();

            – Devon
            Mar 25 at 19:16






          • 1





            Technically, because this is Laravel, OP needs to change $table->string('user_name')->unsigned(); to $table->string('user_name');.

            – Ed Cottrell♦
            Mar 25 at 19:16












          • I edited my post. Even without the name, it still won't work. Now I'm getting this error: General error: 1215 Cannot add foreign key constraint (SQL: alter table bankaccounts add constraint bankaccounts_user_id_foreign foreign key (user_id) references users (id) on delete cascade)

            – idontunderstandarrays
            Mar 25 at 19:17






          • 1





            @idontunderstandarrays That's a different problem and should be asked in a different question.

            – Ed Cottrell♦
            Mar 25 at 19:17











          • @EdCottrell Okay well, I changed it to your suggestion, removed the foreign key for the user_id and I'm also getting that same error.

            – idontunderstandarrays
            Mar 25 at 19:19















          3














          You can't have a "unsigned" varchar.



          You have



          user_name varchar(191) unsigned not null


          change it to



          user_name varchar(191) not null





          share|improve this answer


















          • 2





            In the laravel migration, this is caused by $table->string('user_name')->unsigned();

            – Devon
            Mar 25 at 19:16






          • 1





            Technically, because this is Laravel, OP needs to change $table->string('user_name')->unsigned(); to $table->string('user_name');.

            – Ed Cottrell♦
            Mar 25 at 19:16












          • I edited my post. Even without the name, it still won't work. Now I'm getting this error: General error: 1215 Cannot add foreign key constraint (SQL: alter table bankaccounts add constraint bankaccounts_user_id_foreign foreign key (user_id) references users (id) on delete cascade)

            – idontunderstandarrays
            Mar 25 at 19:17






          • 1





            @idontunderstandarrays That's a different problem and should be asked in a different question.

            – Ed Cottrell♦
            Mar 25 at 19:17











          • @EdCottrell Okay well, I changed it to your suggestion, removed the foreign key for the user_id and I'm also getting that same error.

            – idontunderstandarrays
            Mar 25 at 19:19













          3












          3








          3







          You can't have a "unsigned" varchar.



          You have



          user_name varchar(191) unsigned not null


          change it to



          user_name varchar(191) not null





          share|improve this answer













          You can't have a "unsigned" varchar.



          You have



          user_name varchar(191) unsigned not null


          change it to



          user_name varchar(191) not null






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 19:15









          LulceltechLulceltech

          1,2783 silver badges14 bronze badges




          1,2783 silver badges14 bronze badges







          • 2





            In the laravel migration, this is caused by $table->string('user_name')->unsigned();

            – Devon
            Mar 25 at 19:16






          • 1





            Technically, because this is Laravel, OP needs to change $table->string('user_name')->unsigned(); to $table->string('user_name');.

            – Ed Cottrell♦
            Mar 25 at 19:16












          • I edited my post. Even without the name, it still won't work. Now I'm getting this error: General error: 1215 Cannot add foreign key constraint (SQL: alter table bankaccounts add constraint bankaccounts_user_id_foreign foreign key (user_id) references users (id) on delete cascade)

            – idontunderstandarrays
            Mar 25 at 19:17






          • 1





            @idontunderstandarrays That's a different problem and should be asked in a different question.

            – Ed Cottrell♦
            Mar 25 at 19:17











          • @EdCottrell Okay well, I changed it to your suggestion, removed the foreign key for the user_id and I'm also getting that same error.

            – idontunderstandarrays
            Mar 25 at 19:19












          • 2





            In the laravel migration, this is caused by $table->string('user_name')->unsigned();

            – Devon
            Mar 25 at 19:16






          • 1





            Technically, because this is Laravel, OP needs to change $table->string('user_name')->unsigned(); to $table->string('user_name');.

            – Ed Cottrell♦
            Mar 25 at 19:16












          • I edited my post. Even without the name, it still won't work. Now I'm getting this error: General error: 1215 Cannot add foreign key constraint (SQL: alter table bankaccounts add constraint bankaccounts_user_id_foreign foreign key (user_id) references users (id) on delete cascade)

            – idontunderstandarrays
            Mar 25 at 19:17






          • 1





            @idontunderstandarrays That's a different problem and should be asked in a different question.

            – Ed Cottrell♦
            Mar 25 at 19:17











          • @EdCottrell Okay well, I changed it to your suggestion, removed the foreign key for the user_id and I'm also getting that same error.

            – idontunderstandarrays
            Mar 25 at 19:19







          2




          2





          In the laravel migration, this is caused by $table->string('user_name')->unsigned();

          – Devon
          Mar 25 at 19:16





          In the laravel migration, this is caused by $table->string('user_name')->unsigned();

          – Devon
          Mar 25 at 19:16




          1




          1





          Technically, because this is Laravel, OP needs to change $table->string('user_name')->unsigned(); to $table->string('user_name');.

          – Ed Cottrell♦
          Mar 25 at 19:16






          Technically, because this is Laravel, OP needs to change $table->string('user_name')->unsigned(); to $table->string('user_name');.

          – Ed Cottrell♦
          Mar 25 at 19:16














          I edited my post. Even without the name, it still won't work. Now I'm getting this error: General error: 1215 Cannot add foreign key constraint (SQL: alter table bankaccounts add constraint bankaccounts_user_id_foreign foreign key (user_id) references users (id) on delete cascade)

          – idontunderstandarrays
          Mar 25 at 19:17





          I edited my post. Even without the name, it still won't work. Now I'm getting this error: General error: 1215 Cannot add foreign key constraint (SQL: alter table bankaccounts add constraint bankaccounts_user_id_foreign foreign key (user_id) references users (id) on delete cascade)

          – idontunderstandarrays
          Mar 25 at 19:17




          1




          1





          @idontunderstandarrays That's a different problem and should be asked in a different question.

          – Ed Cottrell♦
          Mar 25 at 19:17





          @idontunderstandarrays That's a different problem and should be asked in a different question.

          – Ed Cottrell♦
          Mar 25 at 19:17













          @EdCottrell Okay well, I changed it to your suggestion, removed the foreign key for the user_id and I'm also getting that same error.

          – idontunderstandarrays
          Mar 25 at 19:19





          @EdCottrell Okay well, I changed it to your suggestion, removed the foreign key for the user_id and I'm also getting that same error.

          – idontunderstandarrays
          Mar 25 at 19:19








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid …


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55344918%2fsql-error-code-1064-with-creating-laravel-foreign-keys%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

          Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?