question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

INSERT INTO not working in migration when database is created from scratch

See original GitHub issue

Runtime Environment SQLDelight version: 1.4.4 Application OS: iOS (so with SQLite)

Describe the bug INSERT INTO sql statements are not executed when placed in a migration file (sqm).

In the beginning of creating the app we placed the CREATE TABLE statement in the sq file but moved that to the sqm file for clarity. This bug is also happening when the CREATE TABLE is in the sq file.

Details Now I have the following two sqm files. 0.sqm

CREATE TABLE User (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL
);

1.sqm

CREATE TABLE UserType (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL
);

INSERT INTO UserType (name) VALUES ("standard");
INSERT INTO UserType (name) VALUES ("admin");
INSERT INTO UserType (name) VALUES ("member");

When the user previously installed the app where 0.sqm is already executed and now is migrated to 1.sqm the INSERT INTO have been executed in the database. So there is a UserType table with 3 entries.

Now when I removed the app from the device (to simulate a new user) and reinstall it the 2 tables have been created but there are no entries in UserType while that is expected.

In the build.gradle.kts file I have set deriveSchemaFromMigrations true

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
AlecStrongcommented, Apr 11, 2022

yes, it already exists. If you set deriveSchemaFromMigrations = true in your sqldelight gradle config then it will treat migration files as the source of truth and you dont need any SQL in your .sq files except runtime statements

0reactions
hopeman15commented, Apr 11, 2022

if you want default values on a fresh install those INSERT INTO statements need to be copied into .sq files as well

@AlecStrong thanks for the quick reply, I must have missed / over read this in the docs 🤔 I applied the changes and it is working as expected.

As a side note, are there any plans to try and eliminate this duplication?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrate work with create table but not work with inset sql
Generally speaking, if you can create a table but not do an insert it's most likely a permission issue on the database user....
Read more >
Migration not inserting data to table? - Stack Overflow
Your error is probably some data validation, for example, the id that should be set automatically by the database. Try using the "insert" ......
Read more >
Code First Migrations with an existing database - EF6
This article covers using Code First Migrations with an existing database, one that wasn't created by Entity Framework.
Read more >
Migrations and Seed Data With Entity Framework Core
NET Core 3.0, EF Core tools required for migrations are not preinstalled. ... created an empty migration that does nothing in our database....
Read more >
Troubleshooting migration tasks in AWS Database Migration ...
Troubleshoot issues when migrating data using AWS Database Migration ... So, when the primary key is created on a LOB column, the initial...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found