With v1.2.5, Tables Created with Sync2 ignores xorm:"extends" tags #2102

Open
opened 2022-01-17 11:17:54 +00:00 by hyorigo · 1 comment

Here's the schema code:

type dbModel struct {
	CreatedAt time.Time  `xorm:"created index" json:"created_at"`
	UpdatedAt time.Time  `xorm:"updated index" json:"updated_at"`
	DeletedAt *time.Time `xorm:"deleted" json:"deleted_at,omitempty" csv:"-"`
}

type dbModelWithAutoID struct {
	dbModel `xorm:"extends"`
	ID      int64 `xorm:"'id' bigint pk autoincr notnull" json:"id"`
}

type Note struct {
	dbModelWithAutoID `xorm:"extends"`
	Title             string `xorm:"'title' longtext notnull" json:"title"`
	Content           string `xorm:"'content' longtext notnull" json:"content"`
	Archived          bool   `xorm:"'archived' bool default(false) index" json:"archived"`
}

type Label struct {
	dbModelWithAutoID `xorm:"extends"`
	Name              string `xorm:"'name' varchar(1024) index unique notnull" json:"name"`
	Archived          bool   `xorm:"'archived' bool default(false) index" json:"archived"`
}

type NoteLabel struct {
	dbModel `xorm:"extends"`
	NoteID  int64 `xorm:"'note_id' bigint pk"`
	LabelID int64 `xorm:"'label_id' bigint pk"`
}

And the code to sync/create tables with SQLite3:

func (db *Database) Init() {
	tables := []interface{}{
		new(Note),
		new(Label),
		new(NoteLabel),
	}
	if err := db.engine.Sync2(tables...); err != nil {
		log.Warnw("fail to initialize db table", "driver", cfg.DatabaseDriver, "source", cfg.DatabaseSource, zap.Error(err))
	} else {
		log.Infow("successfully initialized db tables", "driver", cfg.DatabaseDriver, "source", cfg.DatabaseSource)
	}
}

Using xorm v1.0.5, it creates the expected tables. Here's the SQL dumped from the DB that was created:

create table label
(
    created_at DATETIME,
    updated_at DATETIME,
    deleted_at DATETIME,
    id         INTEGER not null
        primary key autoincrement,
    name       TEXT    not null,
    archived   INTEGER default 0
);

create index IDX_label_archived
    on label (archived);

create index IDX_label_created_at
    on label (created_at);

create index IDX_label_updated_at
    on label (updated_at);

create unique index UQE_label_name
    on label (name);

create table note
(
    created_at DATETIME,
    updated_at DATETIME,
    deleted_at DATETIME,
    id         INTEGER not null
        primary key autoincrement,
    title      TEXT    not null,
    content    TEXT    not null,
    archived   INTEGER default 0
);

create index IDX_note_archived
    on note (archived);

create index IDX_note_created_at
    on note (created_at);

create index IDX_note_updated_at
    on note (updated_at);

create table note_label
(
    created_at DATETIME,
    updated_at DATETIME,
    deleted_at DATETIME,
    note_id    INTEGER not null,
    label_id   INTEGER not null,
    primary key (note_id, label_id)
);

create index IDX_note_label_created_at
    on note_label (created_at);

create index IDX_note_label_updated_at
    on note_label (updated_at);

But after upgraded to v1.2.5, the nested struct tagged with extends are ignored. Here's the SQL dump (created with the exactly same code):

create table label
(
    name     TEXT not null,
    archived INTEGER default 0
);

create index IDX_label_archived
    on label (archived);

create unique index UQE_label_name
    on label (name);

create table note
(
    title    TEXT not null,
    content  TEXT not null,
    archived INTEGER default 0
);

create index IDX_note_archived
    on note (archived);

create table note_label
(
    note_id  INTEGER not null,
    label_id INTEGER not null,
    primary key (note_id, label_id)
);
Here's the schema code: ```go type dbModel struct { CreatedAt time.Time `xorm:"created index" json:"created_at"` UpdatedAt time.Time `xorm:"updated index" json:"updated_at"` DeletedAt *time.Time `xorm:"deleted" json:"deleted_at,omitempty" csv:"-"` } type dbModelWithAutoID struct { dbModel `xorm:"extends"` ID int64 `xorm:"'id' bigint pk autoincr notnull" json:"id"` } type Note struct { dbModelWithAutoID `xorm:"extends"` Title string `xorm:"'title' longtext notnull" json:"title"` Content string `xorm:"'content' longtext notnull" json:"content"` Archived bool `xorm:"'archived' bool default(false) index" json:"archived"` } type Label struct { dbModelWithAutoID `xorm:"extends"` Name string `xorm:"'name' varchar(1024) index unique notnull" json:"name"` Archived bool `xorm:"'archived' bool default(false) index" json:"archived"` } type NoteLabel struct { dbModel `xorm:"extends"` NoteID int64 `xorm:"'note_id' bigint pk"` LabelID int64 `xorm:"'label_id' bigint pk"` } ``` And the code to sync/create tables with SQLite3: ```go func (db *Database) Init() { tables := []interface{}{ new(Note), new(Label), new(NoteLabel), } if err := db.engine.Sync2(tables...); err != nil { log.Warnw("fail to initialize db table", "driver", cfg.DatabaseDriver, "source", cfg.DatabaseSource, zap.Error(err)) } else { log.Infow("successfully initialized db tables", "driver", cfg.DatabaseDriver, "source", cfg.DatabaseSource) } } ``` Using xorm v1.0.5, it creates the expected tables. Here's the SQL dumped from the DB that was created: ```sql create table label ( created_at DATETIME, updated_at DATETIME, deleted_at DATETIME, id INTEGER not null primary key autoincrement, name TEXT not null, archived INTEGER default 0 ); create index IDX_label_archived on label (archived); create index IDX_label_created_at on label (created_at); create index IDX_label_updated_at on label (updated_at); create unique index UQE_label_name on label (name); create table note ( created_at DATETIME, updated_at DATETIME, deleted_at DATETIME, id INTEGER not null primary key autoincrement, title TEXT not null, content TEXT not null, archived INTEGER default 0 ); create index IDX_note_archived on note (archived); create index IDX_note_created_at on note (created_at); create index IDX_note_updated_at on note (updated_at); create table note_label ( created_at DATETIME, updated_at DATETIME, deleted_at DATETIME, note_id INTEGER not null, label_id INTEGER not null, primary key (note_id, label_id) ); create index IDX_note_label_created_at on note_label (created_at); create index IDX_note_label_updated_at on note_label (updated_at); ``` But after upgraded to v1.2.5, the nested struct tagged with `extends` are ignored. Here's the SQL dump (created with the exactly same code): ```sql create table label ( name TEXT not null, archived INTEGER default 0 ); create index IDX_label_archived on label (archived); create unique index UQE_label_name on label (name); create table note ( title TEXT not null, content TEXT not null, archived INTEGER default 0 ); create index IDX_note_archived on note (archived); create table note_label ( note_id INTEGER not null, label_id INTEGER not null, primary key (note_id, label_id) ); ```
Author

I compared with old versions:

  • v1.0.7 - works well with extends
  • v1.1.2 - ignore extends
I compared with old versions: * v1.0.7 - works well with `extends` * v1.1.2 - ignore `extends`
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#2102
No description provided.