pardom-zz / ActiveAndroid

Active record style SQLite persistence for Android
http://www.activeandroid.com
4.7k stars 1.03k forks source link

Error duplicate column name: Id when migrate DB #576

Open nguyenconghoan opened 2 years ago

nguyenconghoan commented 2 years ago

I want add new table Products into current DB.

I have performed the following steps: Step 1: Change files AndroidManifest.xml

<meta-data
    android:name="AA_DB_VERSION"
    android:value="2" />

Step 2: Create new file 2.sql in folder assets/migrations

DROP TABLE IF EXISTS "Products";
CREATE TABLE IF NOT EXISTS "Products" ("Id" INTEGER,"p_name" TEXT,"p_des" TEXT,"p_price" TEXT);

Step 3: Create new file Products.java

@Table(name = "Products")
public class Products extends Model implements Serializable {
    @Column(name = "Id")
    public Integer Id;
    @Column(name = "p_name")
    public String p_name;
    @Column(name = "p_des")
    public String p_des;
    @Column(name = "p_price")
    public String p_price;

    public Products() {
        super();
    }

}

When I update the version app, I get the following error: SQLiteLog: (1) duplicate column name: Id in "CREATE TABLE IF NOT EXISTS Products (Id INTEGER PRIMARY KEY AUTOINCREMENT, Id INTEGER PRIMARY KEY AUTOINCREMENT, p_name TEXT, p_des TEXT, p_price TEXT);"

azizimusa commented 10 months ago
  1. Don't need to add column "Id". AA will auto add it.
  2. You don't need to assign column (@Column(name = "p_price") if the name is same as variable. Just make it "@Column" is enough.