# 3 common foreign key mistakes (and how to avoid them)

DevFeed: [3 common foreign key mistakes (and how to avoid them)](<https://devfeed.tech/articles/3-common-foreign-key-mistakes-and-how-to-avoid-them-23771.md>)

Original publisher: [Read original article](<https://cockroachlabs.com/blog/common-foreign-key-mistakes>)

Author: Charlie Custer

Published: 2023-06-28T00:00:00Z

Content type: tutorial

Language: en

Sources: [Cockroach Labs](<https://devfeed.tech/sources/cockroach-labs.md>)

Topics: [Databases](<https://devfeed.tech/topics/databases.md>), [Database Migration](<https://devfeed.tech/topics/database-migration.md>), [CockroachDB](<https://devfeed.tech/topics/cockroachdb.md>), [data](<https://devfeed.tech/topics/data.md>), [SQLite](<https://devfeed.tech/topics/sqlite.md>)

Tags: [3](<https://devfeed.tech/tags/3.md>), [cockroachdb](<https://devfeed.tech/tags/cockroachdb.md>), [data-type](<https://devfeed.tech/tags/data-type.md>), [database](<https://devfeed.tech/tags/database.md>), [errors](<https://devfeed.tech/tags/errors.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [mistakes](<https://devfeed.tech/tags/mistakes.md>), [schema](<https://devfeed.tech/tags/schema.md>), [sql](<https://devfeed.tech/tags/sql.md>), [sqlite](<https://devfeed.tech/tags/sqlite.md>), [systems](<https://devfeed.tech/tags/systems.md>), [types](<https://devfeed.tech/tags/types.md>)

## AI overview

This tutorial explains three common foreign key mistakes: mismatched data types, dangling references to nonexistent tables or columns, and related schema problems. It describes how these issues can arise and how to prevent them, including database-specific behavior in SQLite and CockroachDB.

## Source excerpt

Foreign keys are an important element of any relational database. But when you're setting up your database schema, it's relatively easy to create problems for yourself if the foreign keys aren't set up correctly. Here are three of the most common mistakes made with foreign keys, and a little advice on how to avoid them!