{"id":109471,"date":"2026-04-27T13:30:00","date_gmt":"2026-04-27T13:30:00","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=109471"},"modified":"2026-04-27T13:44:45","modified_gmt":"2026-04-27T13:44:45","slug":"vibe-coding-and-databases-the-hidden-risks-of-ai-generated-database-code","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/ai\/vibe-coding-and-databases-the-hidden-risks-of-ai-generated-database-code\/","title":{"rendered":"Vibe coding and databases: the hidden risks of AI-generated database code"},"content":{"rendered":"\n<p><strong>\u201cVibe coding\u201d with AI can introduce hidden risks in database development, including security vulnerabilities, broken query logic, and data integrity issues.<\/strong> <\/p>\n\n\n\n<p><strong>In this article,<\/strong> <strong>Chisom Kanu explores how Andrej Karpathy\u2019s \u201cvibe coding\u201d trend reached databases, and what happened next &#8211; with examples of some of the real-world incidents that arose (including examples of 5 critical failure patterns in AI-generated code). Plus, learn how to minimize the risk of using AI tools to generate and edit code.<\/strong><\/p>\n\n\n\n<p>In early February 2025, Andrej&nbsp;Karpathy&nbsp;posted something on X that ended up being quoted across the <a href=\"https:\/\/www.nytimes.com\/2026\/02\/18\/opinion\/ai-software.html\" target=\"_blank\" rel=\"noreferrer noopener\">New York Times<\/a>, <a href=\"https:\/\/arstechnica.com\/ai\/2025\/03\/is-vibe-coding-with-ai-gnarly-or-reckless-maybe-some-of-both\/\" target=\"_blank\" rel=\"noreferrer noopener\">Ars Technica<\/a>, and <a href=\"https:\/\/www.theguardian.com\/technology\/2025\/mar\/16\/ai-software-coding-programmer-expertise-jobs-threat\" target=\"_blank\" rel=\"noreferrer noopener\">The Guardian<\/a> within days. The post described a way of building software where you \u201cfully give in to the vibes, embrace exponentials, and forget that the code even exists.\u201d He called it&nbsp;<a href=\"https:\/\/x.com\/karpathy\/status\/1886192184808149383\" target=\"_blank\" rel=\"noreferrer noopener\">vibe coding<\/a>.&nbsp;<\/p>\n\n\n\n<p>By the end of 2025, <a href=\"https:\/\/www.collinsdictionary.com\/woty\" target=\"_blank\" rel=\"noreferrer noopener\">Collins Dictionary had named it Word of the Year<\/a>. Karpathy&nbsp;had already moved on at that point, writing in a retrospective that the term had been &#8220;a shower of thoughts throwaway tweet&#8221; he never expected to travel this far. He now prefers &#8220;agentic engineering&#8221; for what professional AI-assisted development&nbsp;actually looks&nbsp;like in practice. <\/p>\n\n\n\n<p>However, the cultural moment had already lost its original meaning. What started as a description of weekend tinkering had become shorthand for shipping AI-generated code to production without reading the diffs.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-the-dilemma-what-happens-when-vibe-coding-reaches-the-database-layer\">The dilemma: what happens when vibe coding reaches the database layer<\/h2>\n\n\n\n<p>This article is not about whether <a href=\"https:\/\/www.red-gate.com\/products\/sql-prompt\/#ai-powered-code\" target=\"_blank\" rel=\"noreferrer noopener\">AI coding tools<\/a> are useful. They are. It is, however, about what happens when vibe coding reaches the database layer. This has&nbsp;a very different&nbsp;risk profile from frontend code, API routes, or UI components.<\/p>\n\n\n\n<p>After all, databases store what they accumulate over time. A poorly written <a href=\"https:\/\/www.red-gate.com\/simple-talk\/blogs\/writing-an-efficient-query\/\" target=\"_blank\" rel=\"noreferrer noopener\">query<\/a> might return the wrong results for months before anyone notices. A dropped <a href=\"https:\/\/www.red-gate.com\/simple-talk\/blogs\/working-within-constraints\/\" target=\"_blank\" rel=\"noreferrer noopener\">constraint<\/a> might allow <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/database-administration-sql-server\/finding-data-corruption\/\" target=\"_blank\" rel=\"noreferrer noopener\">corrupt data<\/a> to accumulate. A migration generated by an AI tool and applied without review might alter a schema in&nbsp;ways that break things downstream, quietly, for a long time.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"260\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/04\/image-3.png\" alt=\"An image showing a spectrum of AI-generated code risk, by layer.\" class=\"wp-image-109472\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/04\/image-3.png 624w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/04\/image-3-300x125.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><figcaption class=\"wp-element-caption\"><em>Risk increases sharply when AI-generated code reaches the database layer. Source:&nbsp;<a href=\"https:\/\/www.veracode.com\/state-of-software-security\" target=\"_blank\" rel=\"noreferrer noopener\">Veracode State of Software Security 2025<\/a>&nbsp;<\/em><\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-how-quickly-did-vibe-coding-spread-into-professional-teams\">How quickly did vibe coding spread into professional teams?<\/h3>\n\n\n\n<p>The speed of adoption was striking. By March 2025, <a href=\"https:\/\/www.merriam-webster.com\/dictionary\/vibe%20coding\" target=\"_blank\" rel=\"noreferrer noopener\">Merriam-Webster had listed vibe coding<\/a> as a trending expression.&nbsp;<a href=\"https:\/\/www.coderabbit.ai\/blog\/a-semantic-history-how-the-term-vibe-coding-went-from-a-tweet-to-prod\" target=\"_blank\" rel=\"noreferrer noopener\">Y Combinator reported<\/a>&nbsp;that&nbsp;25 percent of startups in its Winter 2025 cohort had codebases that were 95% AI-generated. <a href=\"https:\/\/aws.amazon.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">AWS (Amazon Web Services)<\/a> even found that more than 25% of its new internal code came from AI tools, with <a href=\"https:\/\/www.itpro.com\/technology\/artificial-intelligence\/sundar-pichai-says-more-than-25-percent-of-googles-code-is-now-generated-by-ai-and-its-a-big-hint-at-the-future-of-software-development\" target=\"_blank\" rel=\"noreferrer noopener\">Microsoft and Google reporting similar findings<\/a>. <\/p>\n\n\n\n<p>Elsewhere, <a href=\"https:\/\/www.wsj.com\/articles\/vibe-coding-has-arrived-for-businesses-5528e942\" target=\"_blank\" rel=\"noreferrer noopener\">The Wall Street Journal<\/a> covered professional software engineers adopting vibe coding for commercial use cases &#8211; not just personal projects.&nbsp;<\/p>\n\n\n\n<p>Just a couple of months later, an application platform designed specifically for vibe coding &#8211; <a href=\"https:\/\/lovable.dev\/\" target=\"_blank\" rel=\"noreferrer noopener\">Lovable AI<\/a> &#8211;&nbsp;<a href=\"https:\/\/www.desplega.ai\/blog\/vibe-break-chapter-iv-the-lovable-inadvertence\" target=\"_blank\" rel=\"noreferrer noopener\">reported<\/a>&nbsp;that approximately 10% of the apps built on its platform had a security issue. This allowed personal information to be accessed by <em>anyone<\/em> &#8211; a significant problem. Was the vibe coding movement over before it had even began?<\/p>\n\n\n\n<p>The signs were not promising. By September, <a href=\"https:\/\/www.fastcompany.com\/91398622\/the-vibe-coding-hangover-is-upon-us\" target=\"_blank\" rel=\"noreferrer noopener\">Fast Company was reporting on what it called the vibe coding hangover<\/a>. Essentially, senior engineers were describing maintenance issues when asked to extend codebases that had been vibe-coded by someone else. The problem here is obvious: when you haven&#8217;t written the code, you probably don&#8217;t understand it. In turn, if and when it breaks, you have no foundation from which to fix it.&nbsp;<\/p>\n\n\n\n<p>None of this is surprising, but the database layer is where the risk&nbsp;concentrates. A bad <a href=\"https:\/\/dev.to\/xavortm\/what-are-components-in-the-front-end-and-why-do-we-need-them-2o2p\" target=\"_blank\" rel=\"noreferrer noopener\">front-end&nbsp;component<\/a>&nbsp;is&nbsp;visible&nbsp;the moment someone loads the page. On the contrary, a query that returns wrong rows is invisible&#8230;until those wrong rows show up in a report, bank statement, or compliance audit.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-what-actually-went-wrong-documented-vibe-coding-incidents-explained\">What actually went wrong? Documented vibe-coding incidents explained<\/h3>\n\n\n\n<p>The most widely cited vibe coding incident in 2025 involved&nbsp;<a href=\"https:\/\/www.saastr.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">SaaStr<\/a>&nbsp;founder Jason Lemkin, who publicly documented how&nbsp;<a href=\"https:\/\/www.fastcompany.com\/91372483\/replit-ceo-what-really-happened-when-ai-agent-wiped-jason-lemkins-database-exclusive\" target=\"_blank\" rel=\"noreferrer noopener\">Replit&#8217;s AI agent deleted a production database<\/a>&nbsp;while working on his project. The agent had unrestricted&nbsp;write&nbsp;access to the environment,&nbsp;made a decision&nbsp;consistent with its task description, and the database was gone. This was, however, a permissions problem &#8211; not an AI-alignment one. The AI agent did exactly what it was&nbsp;<em>permitted<\/em>&nbsp;to do.&nbsp;<\/p>\n\n\n\n<p>Another incident in March of the same year involved a founder who built an entire <a href=\"https:\/\/azure.microsoft.com\/en-gb\/resources\/cloud-computing-dictionary\/what-is-saas\" target=\"_blank\" rel=\"noreferrer noopener\">SaaS<\/a> product with <a href=\"https:\/\/cursor.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Cursor AI<\/a>, so there was no handwritten code. Within two days of launch, he discovered that <a href=\"https:\/\/www.fortinet.com\/uk\/resources\/cyberglossary\/api-key\" target=\"_blank\" rel=\"noreferrer noopener\">API keys<\/a> were exposed in frontend&nbsp;code,&nbsp;the database had no <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/learn\/sql-server-authentication-methods\/\" target=\"_blank\" rel=\"noreferrer noopener\">authentication controls<\/a>, and users were bypassing subscriptions and writing arbitrary records directly to production tables. He shut the application down entirely.&nbsp;<\/p>\n\n\n\n<p>These are not stories about developers being&nbsp;reckless &#8211; they both involved people using well-regarded, popular tools in ways those tools were marketed to support. In December 2025, CodeRabbit analyzed 470 open-source pull requests and <a href=\"https:\/\/www.coderabbit.ai\/blog\/state-of-ai-vs-human-code-generation-report\" target=\"_blank\" rel=\"noreferrer noopener\">found that AI co-authored code produced overall security findings at 1.57 times the rate of human-written code<\/a>. <\/p>\n\n\n\n<p>In particular, cross-site scripting vulnerabilities appeared at 2.74 times the rate, and <a href=\"https:\/\/www.veracode.com\/state-of-software-security\" target=\"_blank\" rel=\"noreferrer noopener\">Veracode<\/a> reported a figure of 45% for critical vulnerabilities in AI-assisted development.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-to-watch-out-for-5-failure-patterns-in-ai-generated-database-code\">What to watch out for: 5 failure patterns in AI-generated database code<\/h2>\n\n\n\n<p>These patterns are not random: they appear repeatedly across vibe coding incident reports. Each one&nbsp;emerges&nbsp;from a specific gap between what AI coding tools <em>know<\/em>, and what production database work <em>requires<\/em>.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"252\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/04\/image-4.png\" alt=\"Image showing 5 AI-generated database failure patterns.\" class=\"wp-image-109474\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/04\/image-4.png 624w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/04\/image-4-300x121.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><figcaption class=\"wp-element-caption\"><em>Five documented failure patterns in AI-generated database code, with specific technical descriptions.<\/em>&nbsp;<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-failure-1-sql-injection-through-string-concatenation\">Failure 1: SQL injection through string concatenation <\/h3>\n\n\n\n<p><a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/database-administration-sql-server\/sql-injection-how-it-works-and-how-to-thwart-it\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQL injection<\/a> is not a new problem. In fact, it appeared on the&nbsp;<a href=\"https:\/\/owasp.org\/www-community\/attacks\/SQL_Injection\" target=\"_blank\" rel=\"noreferrer noopener\">OWASP Top 10<\/a>&nbsp;as far back as 2003, and held the number one position until 2021 (18 years!) <\/p>\n\n\n\n<p><a href=\"https:\/\/www.aikido.dev\/blog\/the-state-of-sql-injections\" target=\"_blank\" rel=\"noreferrer noopener\">Aikido Security&#8217;s State of SQL Injections analysis<\/a>&nbsp;logged&nbsp;over 2,400 SQL injection CVEs (common vulnerabilities and exposures) in open-source projects in 2024, with 2,600 expected in 2025. SQL injection accounted for 6.7% of all vulnerabilities in open-source projects and 10% in closed-source projects that year.&nbsp;<\/p>\n\n\n\n<p>What AI coding tools add to this picture is a specific and repeatable failure mode. When a developer asks an AI to write a query without specifying security requirements, the generated code&nbsp;frequently&nbsp;<a href=\"https:\/\/www.merriam-webster.com\/dictionary\/concatenation\" target=\"_blank\" rel=\"noreferrer noopener\">concatenates<\/a> user input directly into SQL strings. The AI is not ignorant of <a href=\"https:\/\/www.w3schools.com\/sql\/sql_parameterized_queries.asp\" target=\"_blank\" rel=\"noreferrer noopener\">parameterized&nbsp;queries<\/a> &#8211; ask&nbsp;it explicitly to use these, and it will. However, if the prompt does not specify security requirements, the path of least resistance in the training data is string concatenation, so that&#8217;s what is generated.&nbsp;<\/p>\n\n\n\n<p>A prompt like&nbsp;<strong>&#8220;<\/strong>write a query that finds users by username<strong>&#8220;<\/strong>&nbsp;will often produce string concatenation, while a prompt like&nbsp;<strong>&#8220;<\/strong>write a parameterized query that finds users by username using prepared statements for PostgreSQL 16<strong>&#8220;<\/strong>&nbsp;will produce something safe. The difference is entirely in what the developer asks for.&nbsp;<\/p>\n\n\n\n<p><a href=\"https:\/\/www.propelcode.ai\/blog\/sql-injection-orm-vulnerabilities-modern-frameworks-2025\" target=\"_blank\" rel=\"noreferrer noopener\">Propel&#8217;s 2025 analysis of SQL injection in ORM frameworks<\/a>&nbsp;found&nbsp;that 18% of applications using ORMs (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Object%E2%80%93relational_mapping\" target=\"_blank\" rel=\"noreferrer noopener\">object-relational mapping<\/a>) were vulnerable to SQL injection on their first security scan. This is precisely because developers assumed that, in these instances, the ORM had handled the security problem.&nbsp;<\/p>\n\n\n\n<p><em>Key stat: over 2,600 SQL injection CVEs are expected in open-source projects in 2025, up from 2,400 in 2024. AI-generated code that skips parameterized queries feeds directly into this trend. Source:&nbsp;<\/em><a href=\"https:\/\/www.aikido.dev\/blog\/the-state-of-sql-injections\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Aikido Security \/ Propel 2025<\/em><\/a>&nbsp;<\/p>\n\n\n\n<section id=\"my-first-block-block_c9dee8d416d162a196464f174a5d97e7\" class=\"my-first-block alignwide\">\n    <div class=\"bg-brand-600 text-base-white py-5xl px-4xl rounded-sm bg-gradient-to-r from-brand-600 to-brand-500 red\">\n        <div class=\"gap-4xl items-start md:items-center flex flex-col md:flex-row justify-between\">\n            <div class=\"flex-1 col-span-10 lg:col-span-7\">\n                <h3 class=\"mt-0 font-display mb-2 text-display-sm\">Enjoying this article? Subscribe to the Simple Talk newsletter<\/h3>\n                <div class=\"child:last-of-type:mb-0\">\n                                            Get selected articles, event information, podcasts and other industry content delivered straight to your inbox.                                    <\/div>\n            <\/div>\n                            <a href=\"https:\/\/www.red-gate.com\/simple-talk\/subscribe\/\" class=\"btn btn--secondary btn--lg\">Subscribe now<\/a>\n                    <\/div>\n    <\/div>\n<\/section>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-failure-2-hallucinated-schema\">Failure 2: Hallucinated schema<\/h3>\n\n\n\n<p>Researchers studying text-to-SQL generation systems have documented two categories of <a href=\"https:\/\/www.red-gate.com\/blog\/inside-perspectives-what-does-ai-mean-for-redgate\/#:~:text=Hallucinations%3A%20LLMs%20can%20generate%20plausible%20but%20entirely%20made%2Dup%20information.%20We%20saw%20this%20when%20an%20airline%E2%80%99s%20AI%20customer%20service%20bot%20invented%20a%20non%2Dexistent%20refund%20policy.%20The%20company%20was%20held%20responsible%2C%20and%20the%20chatbot%20was%20quickly%20removed.\" target=\"_blank\" rel=\"noreferrer noopener\">hallucination<\/a> in AI-generated database code: schema-based and logic-based.&nbsp;<\/p>\n\n\n\n<p>Schema-based hallucination is when the AI references tables or columns that do not exist. It might generate a <code>JOIN<\/code> on a&nbsp;<code>users_profile<\/code>&nbsp;table when your schema only has&nbsp;a users&nbsp;table, for example. That query fails with an error, which at least surfaces the problem&nbsp;immediately. More dangerous is the subtle version: a column named&nbsp;<code>user_id<\/code>&nbsp;in one context and&nbsp;<code>userid<\/code>&nbsp;in another, or an assumed <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/t-sql-programming-sql-server\/questions-about-primary-and-foreign-keys-you-were-too-shy-to-ask\/\" target=\"_blank\" rel=\"noreferrer noopener\">foreign key<\/a> relationship that the schema doesn&#8217;t actually enforce.&nbsp;<\/p>\n\n\n\n<p>Logic-based hallucination is harder to catch. This is when the AI generates syntactically correct SQL that runs without error but returns semantically wrong data. For example, an <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/eager-aggregation-in-sql-queries\/\" target=\"_blank\" rel=\"noreferrer noopener\">aggregation<\/a> that groups by the wrong column, a <code>JOIN<\/code> that produces a <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/t-sql-programming-sql-server\/query-optimizer-and-cartesian-products\/\" target=\"_blank\" rel=\"noreferrer noopener\">Cartesian product<\/a> because a condition is missing, or even a date&nbsp;filter&nbsp;off by one because of an unspoken time zone assumption. These queries pass a quick functional test, go to production, and return wrong data for weeks or months before anyone&nbsp;correlates&nbsp;the bad output back to the query.&nbsp;<\/p>\n\n\n\n<p>The practical defense is to include your full schema definition as a <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/theory-and-design\/data-control-language-aka-security\/\" target=\"_blank\" rel=\"noreferrer noopener\">DDL (data declaration language)<\/a> block in every&nbsp;prompt, and&nbsp;then verify every generated query against that schema before it runs. This is not&nbsp;additional&nbsp;overhead compared to writing the query&nbsp;yourself &#8211; it&#8217;s just a different form of the review that any non-trivial query needs anyway.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-failure-3-cross-database-syntax-confusion\">Failure 3: Cross-database syntax confusion<\/h3>\n\n\n\n<p>AI coding tools have been trained on SQL from every major database engine. <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQL Server<\/a>, <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL<\/a>, <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/mysql\/\" target=\"_blank\" rel=\"noreferrer noopener\">MySQL<\/a>, <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/oracle-databases\/introduction-to-oracle-database-for-database-professionals\/\" target=\"_blank\" rel=\"noreferrer noopener\">Oracle<\/a>, <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/t-sql-programming-sql-server\/using-sqlite-powershell-sql-server\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQLite<\/a>, and others share the basic <code>SELECT\/INSERT\/UPDATE\/DELETE<\/code> structure, but&nbsp;diverge on&nbsp;functions, operators, and behavior in ways that matter significantly in production.&nbsp;<\/p>\n\n\n\n<p><a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/manipulating-data-in-postgresql-learning-postgresql-with-grant\/#the-returning-clause:~:text=deal%20with%20inheritance.-,The%20RETURNING%20Clause,-One%20piece%20of\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL&#8217;s <code>RETURNING<\/code> clause<\/a>, which lets you return values from rows affected by an <code>INSERT<\/code>, <code>UPDATE<\/code> or <code>DELETE<\/code>, doesn&#8217;t exist in MySQL, for example. And <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/learn\/using-top-clause-in-a-select-statement\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQL Server&#8217;s TOP syntax<\/a> for row limiting differs from PostgreSQL&#8217;s <code>LIMIT<\/code>. <\/p>\n\n\n\n<p>Another example is Oracle&#8217;s <code>ROWNUM<\/code> pseudo-column for pagination, which is different from both. Then there&#8217;s older configurations of MySQL allowing <code>GROUP BY<\/code> queries without full aggregation, whereas other databases don&#8217;t allow this.<\/p>\n\n\n\n<p>An AI generating SQL without explicit engine specification will default to patterns that appear most&nbsp;frequently&nbsp;in its training data. If your production database is&nbsp;PostgreSQL&nbsp;but the generated code&nbsp;contains&nbsp;SQL Server syntax patterns, or vice versa, the query may fail at runtime in ways that only appear under specific conditions. Specifying the engine name, version, and relevant configuration flags in every database-related prompt is not&nbsp;pedantic. It&#8217;s the&nbsp;<em>least<\/em> information needed for the AI to generate correct code for your actual system.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-failure-4-n-1-query-generation\">Failure 4: N+1 query generation<\/h3>\n\n\n\n<p>The <a href=\"https:\/\/docs.sentry.io\/product\/issues\/issue-details\/performance-issues\/n-one-queries\/\" target=\"_blank\" rel=\"noreferrer noopener\">N+1 query<\/a> problem is one of the most common performance issues in database-backed applications. Unfortunately,  AI-generated code produces it with striking regularity.&nbsp;<\/p>\n\n\n\n<p>For instance, a developer asks the AI to write code that retrieves all orders with their associated customer names. The AI generates a function that fetches all orders in one query, then loops through each order and issues a separate query to fetch the customer. On a test dataset of 50 orders, let&#8217;s say, this feels instantaneous.<\/p>\n\n\n\n<p>When looking at a production dataset of 50,000, however, it&#8217;s a different picture. Here, the application issues 50,000 <em>individual(!)<\/em> database queries in a loop and degrades catastrophically.&nbsp;<\/p>\n\n\n\n<p>The correct approach is a <code>JOIN<\/code> that retrieves both sets of data in a single query. After all, the AI knows how to write <code>JOINs<\/code>. Problems arise, however, when the prompt describes data retrieval in a way that naturally maps to application-level iteration (e.g &#8220;for each order, get the customer name&#8221;). The AI then mirrors that iteration structure in the code it generates, producing a loop with individual queries rather than a set-based SQL operation.&nbsp;<\/p>\n\n\n\n<p>This is acute in the context of an ORM, as the AI might generate code that uses an ORM&#8217;s <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/Performance\/Guides\/Lazy_loading\" target=\"_blank\" rel=\"noreferrer noopener\">lazy-loading<\/a> behavior in a loop &#8211; thus producing N+1 queries entirely at the ORM layer. These are invisible when reading the application code, so the code <em>looks<\/em> clean. However, the actual SQL being issued against the database is a flood. <\/p>\n\n\n\n<p>Ultimately,&nbsp;testing on&nbsp;realistic data volumes before production deployment is the only reliable way to catch this before it becomes an incident.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-failure-5-missing-indexes-on-foreign-keys\">Failure 5: Missing indexes on foreign keys<\/h3>\n\n\n\n<p>This is the failure pattern that causes the most long-term&nbsp;damage. It&#8217;s the hardest to detect and the effects of it take the longest to materialize.<\/p>\n\n\n\n<p>When an AI generates a database schema or migration, it typically creates the foreign key constraints correctly. The relationship between orders and customers is defined and enforced. What gets omitted, consistently, is the index on the foreign key column itself. A foreign key constraint tells the database to enforce <a href=\"https:\/\/www.ibm.com\/docs\/en\/informix-servers\/14.10.0?topic=integrity-referential\" target=\"_blank\" rel=\"noreferrer noopener\">referential integrity<\/a>, and an index on that column tells the database how to find rows efficiently when that column appears in a <code>JOIN<\/code> or <code>WHERE<\/code> clause. They are separate concerns, and the AI regularly addresses the first without the second.&nbsp;<\/p>\n\n\n\n<p>The absence of a foreign key index has no visible effect on small datasets. On a large dataset, a query joining orders to customers on&nbsp;<code>customer_id<\/code>, where&nbsp;<code>customer_id<\/code>&nbsp;is an unindexed foreign key&nbsp;column,&nbsp;performs&nbsp;a full table scan for every lookup. A <code>JOIN<\/code> that should&nbsp;execute&nbsp;in milliseconds takes seconds. So, the schema is technically correct, but performance is silently <em>incorrect<\/em> &#8211; and only becomes visible when production data volumes are present.&nbsp;<\/p>\n\n\n\n<p>One important platform note: MySQL automatically creates an index on foreign key columns.&nbsp;<a href=\"https:\/\/www.postgresql.org\/docs\/current\/sql-createtable.html\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL and SQL Server do not<\/a>. If you are generating schema for PostgreSQL or SQL Server and not reviewing every migration for explicit index creation on foreign key columns, this gap will appear in production.&nbsp;<\/p>\n\n\n\n<p><em>Quick check: After any AI-generated schema migration, run a query against your&nbsp;database&#8217;s&nbsp;information schema to list every foreign key column and verify each one has a corresponding index. In PostgreSQL:&nbsp;<code>pg_constraint<\/code>&nbsp;joined to&nbsp;<code>pg_index<\/code>. In SQL Server:&nbsp;<code>sys.foreign_key_columns<\/code>&nbsp;joined to&nbsp;<code>sys.indexes<\/code>.<\/em>&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-why-the-ai-doesn-t-know-what-it-doesn-t-know\">Why the AI doesn&#8217;t know&#8230;what it doesn&#8217;t know<\/h2>\n\n\n\n<p>A&nbsp;<a href=\"https:\/\/arxiv.org\/abs\/2211.03622\" target=\"_blank\" rel=\"noreferrer noopener\">2024 Stanford study on AI-assisted code<\/a>&nbsp;found that developers using AI assistants produced significantly less secure&nbsp;code, but&nbsp;were more likely to believe it <em>was<\/em> secure because of how confidently the AI presented its output. The developers had no basis to question it. There are two reasons why this gap is larger for database code than for other domains.<\/p>\n\n\n\n<p>First, database bugs are durable. A front-end rendering bug is&nbsp;visible&nbsp;the moment a user hits the page. A query that returns wrong rows, or a migration that allows technically valid but semantically corrupt data to accumulate, can run undetected for months. By the time someone traces the wrong business outcome back to the&nbsp;database&nbsp;query, the corrupt data has propagated through reports, exports, and downstream systems.&nbsp;<\/p>\n\n\n\n<p>Second, database code carries context that prompts cannot&nbsp;convey. The AI does not know the cardinality of your relationships, or the distribution of values in your key columns. It also doesn&#8217;t know the query patterns that will dominate at scale, or the indexes that already exist (and why). And it certainly has no idea of the business rules defining what valid data looks like. <\/p>\n\n\n\n<p>As a result, it fills those gaps with the most common patterns in its training data &#8211; patterns from public repositories that may&nbsp;look nothing&nbsp;like your production schema.&nbsp;<\/p>\n\n\n\n<p><a href=\"https:\/\/simonwillison.net\/2025\/Mar\/19\/vibe-coding\/\" target=\"_blank\" rel=\"noreferrer noopener\">Simon Willison<\/a> has written extensively on responsible AI use and drew a distinction worth noting: if an LLM wrote the code and you reviewed it, tested it, and can explain how it works to someone else, that is <em>not<\/em> vibe coding. That&#8217;s just using an AI tool as a drafting assistant.&nbsp;This difference matters practically because it determines whether you know what your database is actually doing.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-the-verdict-and-what-to-do-going-forward\">The verdict (and what to do going forward)<\/h2>\n\n\n\n<p>AI tools are genuinely useful for well-defined database tasks such as drafting boilerplate queries, generating documentation from existing schemas, translating between SQL dialects with human verification, and <a href=\"https:\/\/www.red-gate.com\/products\/sql-prompt\/\" target=\"_blank\" rel=\"noreferrer noopener\">identifying&nbsp;obvious issues in code review<\/a>. In all of these instances, AI reduces the time it takes to produce a first draft.<\/p>\n\n\n\n<p>However, what they don&#8217;t (yet) have is the judgment that database work&nbsp;has&nbsp;always&nbsp;required. Understanding <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/database-administration-sql-server\/exploring-query-plans-in-sql\/\" target=\"_blank\" rel=\"noreferrer noopener\">query plans<\/a>, index design, <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/theory-and-design\/transactions\/\" target=\"_blank\" rel=\"noreferrer noopener\">transaction<\/a> isolation, cardinality estimation, and <a href=\"https:\/\/www.red-gate.com\/products\/redgate-data-modeler\/\" target=\"_blank\" rel=\"noreferrer noopener\">data modeling<\/a> are skills that have not become less important because an AI can produce syntactically valid SQL. What&#8217;s changed is that there is now a new category of work: reviewing AI-generated output specifically for the failure patterns described above.&nbsp;<\/p>\n\n\n\n<p>In August 2025, the&nbsp;<a href=\"https:\/\/openssf.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">OpenSSF published guidance<\/a>&nbsp;stating that developers&nbsp;are responsible for&nbsp;any harm caused by code they ship, regardless of how it was generated. The AI is a production input, so the engineer is accountable for that output.<\/p>\n\n\n\n<p>So, what does good AI usage in this context look like in 2026? Well, there are four practices to implement to substantially reduce risk:<\/p>\n\n\n<div class=\"block-core-list\">\n<ol class=\"wp-block-list\">\n<li>1. Include your full DDL schema in every database-related prompt.<\/li>\n<\/ol>\n<\/div>\n\n\n<p>2. Specify the engine and version.<\/p>\n\n\n\n<p>3. Ask the AI to explain its reasoning for <code>JOIN<\/code> conditions, index choices, and transaction boundaries.<\/p>\n\n\n\n<p>4. Never give an AI agent write access to a production database without explicit confirmation steps and an audit trail.<\/p>\n\n\n\n<section id=\"my-first-block-block_5dc50ce6403d39f932e07e478593ba46\" class=\"my-first-block alignwide\">\n    <div class=\"bg-brand-600 text-base-white py-5xl px-4xl rounded-sm bg-gradient-to-r from-brand-600 to-brand-500 red\">\n        <div class=\"gap-4xl items-start md:items-center flex flex-col md:flex-row justify-between\">\n            <div class=\"flex-1 col-span-10 lg:col-span-7\">\n                <h3 class=\"mt-0 font-display mb-2 text-display-sm\">Simple Talk is brought to you by Redgate Software<\/h3>\n                <div class=\"child:last-of-type:mb-0\">\n                                            Take control of your databases with the trusted Database DevOps solutions provider. Automate with confidence, scale securely, and unlock growth through AI.                                    <\/div>\n            <\/div>\n                            <a href=\"https:\/\/www.red-gate.com\/solutions\/overview\/\" class=\"btn btn--secondary btn--lg\">Discover how Redgate can help you<\/a>\n                    <\/div>\n    <\/div>\n<\/section>\n\n\n<section id=\"faq\" class=\"faq-block my-5xl\">\n    <h2>FAQs: Vibe coding and databases: the hidden risk of AI-generated database code<\/h2>\n\n                        <h3 class=\"mt-4xl\">1. What is vibe coding?<\/h3>\n            <div class=\"faq-answer\">\n                <p data-start=\"53\" data-end=\"264\">Vibe coding is a term coined by <span class=\"hover:entity-accent entity-underline inline cursor-pointer align-baseline\"><span class=\"whitespace-normal\">Andrej Karpathy<\/span><\/span> describing a style of AI-assisted development where developers rely heavily on generated code with minimal review.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">2. Why is vibe coding risky for databases?<\/h3>\n            <div class=\"faq-answer\">\n                <p data-start=\"266\" data-end=\"470\">Databases store long-term data, so errors in AI-generated SQL can silently corrupt data, degrade performance, or introduce security vulnerabilities over time.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">3. Can AI-generated SQL cause security issues?<\/h3>\n            <div class=\"faq-answer\">\n                <p data-start=\"472\" data-end=\"682\">Yes. Common risks include SQL injection, exposed data, and missing access controls &#8211; especially when queries are generated without explicit security requirements.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">4. What are the most common failures in AI-generated database code?<\/h3>\n            <div class=\"faq-answer\">\n                <p data-start=\"684\" data-end=\"910\">Frequent issues include SQL injection, incorrect schema assumptions, cross-database syntax errors, N+1 query problems, and missing indexes on foreign keys.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">5. Is AI-generated code safe to use in production?<\/h3>\n            <div class=\"faq-answer\">\n                <p data-start=\"912\" data-end=\"1075\">It can be, but only with thorough human review, testing, and validation &#8211; especially for database-related code.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">6. How can developers reduce risks when using AI for SQL?<\/h3>\n            <div class=\"faq-answer\">\n                <p data-start=\"1077\" data-end=\"1281\">Provide full schema context, specify the database engine, review all queries and migrations, and test against realistic data before deployment.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">7. Do AI coding tools understand database performance?<\/h3>\n            <div class=\"faq-answer\">\n                <p data-start=\"1283\" data-end=\"1486\">Not fully. They often miss critical factors like indexing, query optimization, and data distribution, which are essential for production systems.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">8. What is the safest way to use AI with databases?<\/h3>\n            <div class=\"faq-answer\">\n                <p data-start=\"1488\" data-end=\"1702\">Use AI as a drafting tool, not an authority. Always verify outputs, enforce strict permissions, and avoid giving AI direct write access to production databases.<\/p>\n            <\/div>\n            <\/section>\n","protected":false},"excerpt":{"rendered":"<p>Explore how Andrej Karpathy\u2019s \u201cvibe coding\u201d trend reached databases. Uncover risks, real incidents, and 5 critical failure patterns in AI-generated SQL.&hellip;<\/p>\n","protected":false},"author":342096,"featured_media":103714,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[159169,143523,53],"tags":[159075,4168,4150,4151],"coauthors":[159002],"class_list":["post-109471","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ai","category-databases","category-featured","tag-ai","tag-database","tag-sql","tag-sql-server"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/109471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/342096"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=109471"}],"version-history":[{"count":7,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/109471\/revisions"}],"predecessor-version":[{"id":110164,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/109471\/revisions\/110164"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media\/103714"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=109471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=109471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=109471"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=109471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}