---
title: "Database schema & key types — Addresses for Laravel | Roundly"
description: "The addresses table column by column, and how key_type shapes the polymorphic owner column for bigint, UUID or ULID models."
url: https://roundly-consulting.com/open-source/docs/addresses-for-laravel/database-schema
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Addresses for Laravel](https://roundly-consulting.com/open-source/docs/addresses-for-laravel.md)

# Database schema & key types

The migration creates one table, addresses. Every owner shares it through the addressable morph pair:

```php
$keyType = KeyType::fromConfig('addresses.key_type');

Schema::create('addresses', function (Blueprint $table) use ($keyType): void {
    $table->id();
    $table->morphKey('addressable', $keyType, nullable: false);   // morphs / uuidMorphs / ulidMorphs
    $table->boolean('is_primary')->default(false);
    $table->string('type')->default('default');
    $table->string('name')->nullable();
    $table->string('city')->nullable();
    $table->string('street')->nullable();
    $table->string('postal_code')->nullable();
    $table->string('country_iso')->nullable();
    $table->jsonb('meta')->nullable();
    $table->timestamps();
    $table->softDeletes();
});

$this->onePrimaryPerOwnerAndType();   // PostgreSQL and SQLite: partial unique index
```

| Column | Type | Notes |
| --- | --- | --- |
| `id` | bigint, auto-increment | Primary key — always an integer, whatever key\_type says. |
| `addressable_type / addressable_id` | morph pair | The owner. The id column follows key\_type; indexed together; not nullable. |
| `is_primary` | boolean | Defaults to false. |
| `type` | string | Defaults to 'default'; stores the AddressType value. |
| `name` | string, nullable | Optional label, e.g. HQ. |
| `city, street, postal_code` | string, nullable | The address lines — required by the builder and DTO, nullable in the schema. |
| `country_iso` | string, nullable | ISO code, normalised on write. |
| `meta` | jsonb, nullable | Free-form extras; cast to a Collection. |
| `created_at, updated_at` | timestamps | — |
| `deleted_at` | timestamp, nullable | Soft deletes. |

- There is no country\_name column — the name is resolved at read time by your CountryResolver.
- At most one live primary per owner and type: the package’s actions promote under a lock on the owner + type group, and on PostgreSQL and SQLite the migration also adds the partial unique index addresses\_one\_primary\_per\_type on (addressable\_type, addressable\_id, type) where is\_primary and deleted\_at is null — soft-deleted rows hold no slot.
- The migration is forward-only — it has no down() method.

## Owner key types

key\_type describes the models that own addresses — never the addresses table itself, which always keeps an auto-incrementing id. It picks the morph column the migration creates:

| key\_type | addressable\_id column | Use when |
| --- | --- | --- |
| `bigint` | unsigned big integer — morphs() | Owners use auto-incrementing keys. The default. |
| `uuid` | uuid — uuidMorphs() | Owners use Laravel’s HasUuids. |
| `ulid` | char(26) — ulidMorphs() | Owners use Laravel’s HasUlids. |

All models that own addresses must share one key type. Set it before you migrate — an unrecognised value throws InvalidConfigurationException when the migration runs, naming the key and the value:

```bash
# .env — set before you publish and run the migration
ADDRESSES_KEY_TYPE=uuid
```

```php
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Addresses\Traits\HasAddresses;

class Customer extends Model
{
    use HasAddresses;
    use HasUuids;
}
```

## SQLite table rebuilds

On SQLite, Laravel alters a table by rebuilding it — a later migration that calls ->change() on an addresses column, or adds or drops a foreign key or the primary key there. The rebuild drops the index condition, so addresses\_one\_primary\_per\_type comes back as a full unique index on (addressable\_type, addressable\_id, type), and a second plain address of the same type for one owner then fails with a UniqueConstraintViolationException — typically in host test suites that run on SQLite. Recreate the partial index at the end of such a migration (add your connection’s table prefix to both names if it has one):

```php
// at the end of up() in a migration that rebuilds the addresses table on SQLite
if (DB::connection()->getDriverName() === 'sqlite') {
    DB::statement('drop index if exists "addresses_one_primary_per_type"');
    DB::statement('create unique index "addresses_one_primary_per_type" on "addresses" ("addressable_type", "addressable_id", "type") where "is_primary" and "deleted_at" is null');
}
```

PostgreSQL alters the column in place and keeps the condition; on MySQL the migration never creates this index.

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/addresses-for-laravel.md)
