Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel table * has no column named *

My unit tests recently started failing. I am getting this error:

PDOException: SQLSTATE[HY000]: 
General error: 1 table loan_details has no column named start_month

The line where it is happening, I have this code:

$loan = LoanDetails::create(['loan_percentage' => .8,
        'loan_product_id' => 1,
        'interest_rate' => .5,
        'start_month' => 0,
        'term' => 120,
        'fixed_finance_fee' => 0,
        'variable_finance_Fee' => 0,
        'valid_from' => '2015-01-01'
    ]);

If I comment out the "start_month" line then it logically works.

In the setup of my unit tests I run all migrations (about 80).

I have a migration that looks like this:

Schema::table('loan_details', function(Blueprint $table){
     $table->integer('start_month')->unsigned()->after('interest_only')->default(0);
     $table->decimal('balloon_percent',4,3)->after('term')->nullable();
     $table->integer('balloon_month')->after('balloon_percent')->nullable();
     $table->dropColumn('ordinal_rank');
});

So, I wondered if all the migrations weren't running so I ran this code:

$rows = DB::table('migrations')->get();
print_r($rows);

This lists all of the migrations as having completed. I am using an in memory sqlite db for the tests.

I am wondering if the migrations are run asynchronously and they aren't all finished by the time my code runs? Or if the migrations are silently failing somewhere?

I've been at this for hours and don't know what is happening.

*UPDATE I have a migration that runs AFTER the above migration, and I confirmed that the subsequent migration was successful. So it is just this one migration that is silently failing in some way.

like image 864
ajon Avatar asked Feb 10 '17 17:02

ajon


People also ask

How does Laravel determine the relationship name of a model?

By default, Laravel will determine the relationship associated with the given model based on the class name of the model; however, you may specify the relationship name manually by providing it as the second argument to the whereBelongsTo method:

How to count the number of possible polymorphic types in Laravel?

This will instruct Laravel to retrieve all of the possible polymorphic types from the database. Laravel will execute an additional query in order to perform this operation: Sometimes you may want to count the number of related models for a given relationship without actually loading the models. To accomplish this, you may use the withCount method.

Is there a column named ‘name’ in MySQL?

So, there is no column named ‘name’. In order to be able to insert the record by setting the value of the attribute named ‘name’, the table must be changed so that it possess a column named ‘name’. It can be done by doing it with two ways :

What are the problems with database migrations in Laravel?

One of the problem with database migrations in Laravel is developers rarely actually test “down” migrations – even when testing we usually re-generate the whole schema from scratch instead of launching down () functions. Therefore I would advice to check something before doing down migrations.


Video Answer


2 Answers

I found the issue. It is because of the ridiculous limitation that sqlite has of not having multiple add column statements in one table call as seen here.

When I separate out the migration as below it works:

Schema::table('loan_details', function(Blueprint $table){
    $table->integer('start_month')->unsigned()->after('interest_only')->default(0);
});
Schema::table('loan_details', function(Blueprint $table){
    $table->decimal('balloon_percent',4,3)->after('term')->nullable();
});
Schema::table('loan_details', function(Blueprint $table){
    $table->integer('balloon_month')->after('balloon_percent')->nullable();
});
Schema::table('loan_details', function(Blueprint $table){
    $table->dropColumn('ordinal_rank');
});
like image 109
ajon Avatar answered Oct 18 '22 02:10

ajon


To check SQLite DB records here

There are many other useful built-in dot commands -- see the documentation at http://www.sqlite.org/sqlite.html, section Special commands to sqlite3.

also, check DB schema

like image 31
Ajay Avatar answered Oct 18 '22 03:10

Ajay