Cant create relationship in laravel migrations - laravel

I am trying to make a relationship between "user_id" in members table and "id" in users table. Getting errno: 150 "Foreign key constraint is incorrectly formed". I am migrating users table before the members table.
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');

Your syntax is correct, so it's likely one of a couple problems:
Your users table is not actually called users.
You actually created the members migration before the users migration, and then went back and edited the members migration. Laravel runs migrations in the order of the timestamp in the filename, so you'll need to either edit the file name of your members migration, or create a new migration with your foreign key definition in it.
You're trying to run existing migrations over the top of an existing database. Run php artisan migrate:refresh to roll everything back and run it all over again. Be aware, this will destroy all your data, so make sure it's in a test environment.

Related

How to use the default laravel authentication change using a different database name?

Laravel by default asks for a db called "laravel" and a users table called "users", but I named them in a different way and I want to make it works.
Below the error it gives me:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.users' doesn't exist
EDIT: I kept the default config/database.php, changing nothing inside of it.
You need to update your database name in config/database.php (check connections array) to not user laravel database. Please check .env file too, for laravel database name references.
To not use users table you need to update your User class's $table property. (User class comes with default laravel installation).
RegisterController/#validator method need to be updated to for a different table.

Laravel Cashier - publish migration results in "cannot declare class CreateCustomersColumns"

I have a fresh Laravel installation and I've added Cashier to my project.
Since the Users model on my app won't have a stripe connection, but rather an Accounts model, I need to alter their migration to add columns to Accounts instead of Users
The documentation says to run:
php artisan vendor:publish --tag="cashier-migrations"
which adds the two migration files to database/migrations
From there I can change users to accounts in the migration file.
When I try to run php artisan migrate, I get:
Whoops\Exception\ErrorException : Cannot declare class CreateCustomerColumns, because the name is already in use
This problem only goes away when I delete the migration files, but then the new columns are added to users.
The documentation states that you can disable their migration files by putting Cashier::ignoreMigrations(); in AppServiceProvider
I didn't realize that's what I wanted to do. I thought the publish command only published the two files I needed to edit, however, those are the only migration files that come with Cashier.
Be sure to add Cashier::ignoreMigrations(); in the register method.
And add use Laravel\Cashier\Cashier;
The file (and thus the class) must exist twice, probably in your database/migrations directory, most likely with 2 different datetime prefixes on the filenames.

import Northwind db to laravel

I am trying to import/use Northwind db in my Laravel project.
Since when creating Laravel project you're required to create models, migrations and such, i want to ask, is there some automatized way of importing it, with model and migration creating, or that is only doable by hand?
Another question: I tried adding foreign key constraint to one the tables, and also tried tried to migrate that specific change by creating subfolder in migrations:
php artisan migrate --path=/database/migrations/north/
where "north" is subfolder in which is specific migration is located and
Schema::table('order_details', function (Blueprint $table) {
$table->unsignedBigInteger("OrderID")->nullable();
$table->foreign("OrderID")->references("OrderID")->on("orders")->onDelete("cascade");
});
is code for creating foreign key.
Questions:
Is there automatized way for importing Northwind or any db not created in Laravel, or i have to do all by hand?
If i have to do by hand, then why the way of adding fk constraint doesn't work? Am i missing something? Explanation: When i try to migrate, it says: "Nothing to migrate".

How does Laravel keeping track of batch value?

I created some migration files which created the table "users" and "user_info".
Running php artisan migrate created the tables and added them to the migrations table in the database.
Then I needed to create a new column and add some new rows to the user_info table. So I created a new migration file with the same name as the first migration file which was used to create the "user_info" table.
Since Laravel adds a timestamp to the file I didn't think that would create any problems. However, in the the new migration file I used the same class name as for the the first migration file, did the changes I wanted and finally ran php artisan migrate again.
That seemed to create problems since I got an error saying that the class name was already in use.
So I changed the migration filename to be user_info2 and used the class name UserInfo2 and tried again.
This time it worked and the batch has been bumped up to 2 in the migration table in the database.
But how does Laravel know that the migration file actually is related to the first user_info migration file since I changed both the filename and class name? I thought they had to have the same name in order to make Laravel have control of the "batch-value".
Thanks for any help!
The batch number of the migrations is used to specify multiple migrations that were created while running php artisan migrate.
So for example you create two migrations: users and user_info you run php artisan migrate this two migrations will be created and will be referenced by the same batch number: 1.
Then you create another migration, when you migrate this migration will have the number 2 as batch number.
When you do php artisan migrate:rollback based on the batch number Laravel knows which migrations to rollback, in this case only one migration with the batch number of 2.
So batch in this terms means an array of migrations that has not been run. Those will be referenced by the same ID, in order to make the rollback possible.
Hope this explains a bit more.

Database migrations in Grails

Suppose I have a database containing 3 tables in a grails application:
User
Activity
Friend
User table table has one to many relation to Activity and Friend tables so in User table I have:
static hasMany = [activies: Activity, friends: Friend]
and in Friend and Activity I have:
static belongsTo User.
Application is deployed and lets says thousands of customers have registered. Now changes are made in the database: Table Activity is dropped. A table Journal is created which is on the many sides of the User table. The User table has a new column added and this column cannot be null. An old column in Friend table is deleted that was also defined as not null.
So given above scenario and assume using MySQL what needs to be done to make above changes without deleting existing customers data and safely add the new table to existing customers?
Ruby on Rails comes with ActiveRecord for database migrations. Does Grails comes with something like this out of the box?
Currently in development when I run my grails application after adding a new not null column to a table, I get column cannot be null exception thrown unless I delete that table in the database before running grails application which would recreate the table if not exists. Obviously once the application is deployed I will not have the luxury to delete the table.
Unfortunately, the current version of Grails doesn't come with database migration. However, there is a plugin for Liquibase which makes migrations possible.
The next version of Grails (1.4, planned for Q1 2011) will supposedly contain a built-in migration tool, which I am very much looking forward to.
Note: I haven't used the Liquibase plugin, so I don't have any firsthand experience with it. I have seen numerous blog posts describing its use, however, and I'm probably going to use it in my next Grails project if 1.4 isn't out by then.

Resources