Database contracts | laravel-tags-rust | Spatie       

 SPATIE  

laravel-tags-rust
=================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-tags-rust](https://spatie.be/docs/laravel-tags-rust/v1)  Database contracts

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-tags-rust/v1) 

  Database contracts    
- [ Introduction ](https://spatie.be/docs/laravel-tags-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-tags-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/laravel-tags-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-tags-rust/v1/usage)
- [ Custom models ](https://spatie.be/docs/laravel-tags-rust/v1/custom-models)
- [ Database contracts ](https://spatie.be/docs/laravel-tags-rust/v1/database)
- [ Changelog ](https://spatie.be/docs/laravel-tags-rust/v1/changelog)
- [ Testing ](https://spatie.be/docs/laravel-tags-rust/v1/testing)

 Database contracts
==================

###  On this page 

1. [ Stored rows ](#content-stored-rows)
2. [ Transactions and errors ](#content-transactions-and-errors)
3. [ Concurrent writes ](#content-concurrent-writes)
4. [ Topic queries ](#content-topic-queries)
5. [ Database drivers ](#content-database-drivers)

Stored rows
-----------------------------------------------------------------------------------------

- `tags` contains an integer `id`, JSON `name` and `slug`, nullable `type`, nullable integer `order_column`, and nullable `created_at` / `updated_at`.
- `taggables` contains `tag_id`, `taggable_id`, and `taggable_type`. Its unique key covers all three columns.
- Reading a tag does not rewrite its slug, translations, order or timestamps.
- Historical slugs remain valid until a name update regenerates the slug map. Decide whether to preserve redirects before renaming existing tags.
- Existing IDs and morph aliases are used directly. Imports can retain null order values and timestamps.
- The schema helper only creates new tables; it performs no automatic migration or normalization.
- New names must be a nonempty JSON object with nonblank string values.
- Locale keys accept ASCII letters, digits, hyphens and underscores so they cannot inject JSON path operators.
- Unicode names remain in JSON; new slugs transliterate to lowercase ASCII and replace separators with hyphens.

Transactions and errors
-----------------------------------------------------------------------------------------------------------------------------

- Run database operations within a framework request, command, or `Application::scope`.
- Operations follow the selected tag model's native connection metadata, or the scoped default when it has no named connection.
- Owners, tags and assignments must share that connection. Cross-database tag relations are not supported. The application owns connection configuration.
- Assignment writes validate IDs and run in transactions. A failed name sync rolls back newly created tags as well as pivots.
- A scoped sync cannot detach another namespace or another morph type.
- Callers may wrap several operations in `db::transaction`; nested operations join the transaction through framework savepoints.

Functions return `laravel::Result`. Invalid package input is represented by `spatie_tags::Error` inside the framework error; database and translatable errors retain their own types. Invalid writes do not silently succeed. The package does not swallow database failures.

```
use spatie_tags::{Error, Tags};

let error = Tags::new("en->secret").unwrap_err();
assert!(matches!(error.downcast_ref::<Error>(), Some(Error::InvalidLocale(_))));
```

- `InvalidNames` rejects empty objects, blank names and values that are not strings.
- `InvalidLocale` rejects empty locales and characters outside ASCII letters, digits, hyphens and underscores.
- `UnknownTag` rejects assignment or ordering IDs that do not exist.
- `TypeMismatch` rejects tags outside a type-scoped sync's namespace.
- `InvalidOrder` rejects duplicate IDs or positions that cannot fit an `i32`.

Concurrent writes
-----------------------------------------------------------------------------------------------------------

- The JSON schema has no uniqueness constraint for arbitrary locale names or slugs. `find_or_create` deduplicates sequential calls, not concurrent requests.
- Applications that require strict uniqueness must serialize tag creation, for example through a single tagging job.
- Distinct names may generate the same slug. Imported duplicate matches use display order then ID; review collisions before enabling a new locale.
- Serialize concurrent sync operations on the same owner. A transaction guarantees rollback of a failed operation, not last-writer-wins semantics across concurrent syncs.
- Polymorphic owner existence is the caller's responsibility.

Topic queries
-----------------------------------------------------------------------------------------------

- Build topic counts by joining `taggables` to your posts table, restricting `taggable_type`, published status and archived flags.
- Related-topic joins must restrict both pivot aliases to the same morph type.
- The package does not embed blog-specific publication rules in `Tag`.

Database drivers
--------------------------------------------------------------------------------------------------------

- SQLite behavior is covered by the database integration suite, including reopening a file database.
- Driver features enable MySQL and PostgreSQL. Runtime checks on those engines are tracked separately in [testing](testing).
- SQLite LIKE is not a Unicode case-folding engine.
- Exact-name case sensitivity follows the database collation. Use canonical names in callers when cross-engine consistency matters.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
