greenrobot / greenDAO

greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.
http://greenrobot.org/greendao/
12.63k stars 2.89k forks source link

why @toMany not add foreign key constraint automatically【Chinese Version:tomany在create table语句中为啥不会自动增加约束?】 #1059

Open feidegenggao opened 3 years ago

feidegenggao commented 3 years ago

https://greenrobot.org/greendao/documentation/relations/

this code not working。my code is:

@Entity(nameInDb = "ProjectInfo")
public class ProjectInfo {
    @Id(autoincrement = true)
    private Long id;
    @NotNull
    private String name;
    private String absPath;
    private Date time;

    @ToMany(referencedJoinProperty = "projectId")
    private List<Point> pointList;
@Entity
public class Point {
    @Id
    private Long id;
    private long projectId;
    private String name;
    @ToOne(joinProperty = "projectId")
    private ProjectInfo projectInfo;
// greendao generated code:↓
class PointDao{
    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"POINT\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"PROJECT_ID\" INTEGER NOT NULL ," + // 1: projectId
                "\"NAME\" TEXT);"); // 2: name
    }
}

Question: why create table not cotain foreign key constraint like this:

CREATE TABLE POINT
(
... 
    CONSTRAINT PROJECT
    FOREIGN KEY (PROJECT_ID)  
    REFERENCES PROJECT(PROJECT_ID)  
)