Skip to content

Commit

Permalink
fix: Add flag to allow SQLite to use foreign keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dexters1 committed Jan 23, 2025
1 parent d4453e4 commit de19016
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,17 @@ async def insert_data(
return 0

try:
# Dialect-agnostic table reference
if self.engine.dialect.name == "sqlite":
table = await self.get_table(table_name) # SQLite ignores schemas
else:
table = await self.get_table(table_name, schema_name)

# Use SQLAlchemy Core insert with execution options
async with self.engine.begin() as conn:
# Dialect-agnostic table reference
if self.engine.dialect.name == "sqlite":
# Foreign key constraints are disabled by default in SQLite (for backwards compatibility),
# so must be enabled for each database connection/session separately.
await conn.execute(text("PRAGMA foreign_keys=ON"))
table = await self.get_table(table_name) # SQLite ignores schemas
else:
table = await self.get_table(table_name, schema_name)

result = await conn.execute(table.insert().values(data))

# Return rowcount for validation
Expand Down

0 comments on commit de19016

Please sign in to comment.