Expression is not allowed as field default value

Expression is not allowed as field default value

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Expression is not allowed as field default value

When building a database schema, you often encounter an error message stating "Expression is not allowed as field default value." This specific exception usually surfaces when attempting to define a column's default value using complex logic, such as referencing other columns or executing functions directly in the migration file or schema definition. However, looking at the source material provided, there is a distinct confusion between database constraints and PHP class instantiation that leads to similar-looking errors if not addressed correctly.

The error "Expression is not allowed as field default value" typically originates from the database layer (like MySQL or PostgreSQL) when you try to set a column default using something other than a literal value or a simple function like CURRENT_TIMESTAMP. For example, trying to define a foreign key that references another table's primary key within the same migration using a raw expression often triggers this. The database engine expects a static definition, not a dynamic calculation.

However, the source material presents a different scenario entirely involving PHP and the Yii framework (or a similar legacy MVC structure). The code snippet shows:

class UserController extends XController
{
    var $app = Yii::app();;
    public function init()
    {
        $test = $app;
    }
}

Here, the developer is trying to make the application instance available throughout the class. They define a property $app and attempt to assign Yii::app() to it. The error "Unidentified variable $app" on line 5 (inside the init method) indicates a fundamental misunderstanding of PHP scope and property visibility, rather than a database constraint issue.

In PHP 5 and earlier versions (implied by the use of var), properties were public by default. However, defining a property inside a class does not automatically make it accessible as a static method or before the object is instantiated. More critically, accessing $app inside the init() method relies on $this->app, but the code attempts to access $app directly. In PHP, you cannot access an instance property using just its name without the $this keyword unless you are inside a closure capturing it or in a very specific static context which doesn't apply here.

The double semicolon Yii::app();; is also syntactically incorrect and would throw a parse error before any logic runs, though modern PHP versions might be lenient with trailing punctuation depending on the specific parser version. Assuming that was a typo for a single semicolon, the real issue is scoping.

To fix this in a Yii 1.x context, you should access the application instance via $this->app or pass it explicitly if the framework's architecture requires it. If Yii::app() returns an instance of an object, assigning it to a class property works fine once the object is instantiated. The error "Unidentified variable" suggests that the environment might be trying to treat this as a static context where $this is not defined, or the IDE/linter is flagging that the variable $app used inside init() has not been referenced correctly as a property (e.g., using $app instead of $this->app).

If you are migrating from legacy frameworks to modern stacks like Laravel, this pattern of accessing a global application instance directly in controllers often feels clunky. In Laravel, for instance, dependency injection handles this automatically via the Service Container. You would typically inject the Application or specific services into your controller's constructor rather than storing a reference to the entire app instance as a class variable.

Consider the trade-off: keeping $app as a global-like property in every controller increases coupling and makes unit testing difficult because you cannot easily mock the application state without injecting it. By relying on framework-provided accessors or dependency injection, you keep your code decoupled and testable.

If you encounter database-specific "Expression is not allowed" errors while working with migrations, ensure you are using raw values for defaults in MySQL:

$this->createTable('users', function (Schema $table) {
    $table->integer('status')->default(0); // Allowed
    // $table->integer('score')->default(DB::raw('COALESCE((SELECT MAX(score) FROM users), 0)')); // Not allowed in some contexts
});

The database layer is strict about what can be a default. It expects literals or specific timestamp functions, not arbitrary expressions that require execution logic. Stick to simple defaults for static values and use triggers or application-level logic for complex calculations on insert.

Our network