inert后的CreatedAt不会赋值给指针 #1184

Closed
opened 2019-01-06 14:23:57 +00:00 by axetroy · 3 comments
axetroy commented 2019-01-06 14:23:57 +00:00 (Migrated from github.com)

下面是一条插入数据的 demo

	n := model.Notification{
		Id:      id.Generate(),
		Tittle:  input.Tittle,
		Content: input.Content,
		Status:  model.NotificationStatusActive,
	}

	if _, err = session.Insert(&n); err != nil {
		return
	}

	fmt.Printf("%+v\n", n)

这是输出的 SQL

[xorm] [info]  2019/01/06 22:12:22.976768 [SQL] BEGIN TRANSACTION
[xorm] [info]  2019/01/06 22:12:22.976868 [SQL] SELECT "id", "username", "name", "password", "is_super", "status", "created_at", "updated_at", "deleted_at" FROM "admin" WHERE "id"=$1 AND ("deleted_at" IS NULL OR "deleted_at"=$2) LIMIT 1 []interface {}{"87747873059373056", "0001-01-01 00:00:00"}
[xorm] [info]  2019/01/06 22:12:22.983206 [SQL] INSERT INTO "notification" ("id","tittle","content","status","note","notification_mark_id","created_at","updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) []interface {}{"88409259305336832", "test", "test", 0, "", interface {}(nil), "2019-01-06 22:12:22", "2019-01-06 22:12:22"}
{Id:88409259305336832 Tittle:test Content:test Status:0 Note: Mark:<nil> CreatedAt:0001-01-01 00:00:00 +0000 UTC UpdatedAt:0001-01-01 00:00:00 +0000 UTC DeletedAt:<nil>}
[xorm] [info]  2019/01/06 22:12:22.988461 [SQL] COMMIT

可以看到传入的 &n没有被赋值时间

CreatedAt:0001-01-01 00:00:00 +0000 UTC

下面是一条插入数据的 demo ```go n := model.Notification{ Id: id.Generate(), Tittle: input.Tittle, Content: input.Content, Status: model.NotificationStatusActive, } if _, err = session.Insert(&n); err != nil { return } fmt.Printf("%+v\n", n) ``` 这是输出的 SQL ``` [xorm] [info] 2019/01/06 22:12:22.976768 [SQL] BEGIN TRANSACTION [xorm] [info] 2019/01/06 22:12:22.976868 [SQL] SELECT "id", "username", "name", "password", "is_super", "status", "created_at", "updated_at", "deleted_at" FROM "admin" WHERE "id"=$1 AND ("deleted_at" IS NULL OR "deleted_at"=$2) LIMIT 1 []interface {}{"87747873059373056", "0001-01-01 00:00:00"} [xorm] [info] 2019/01/06 22:12:22.983206 [SQL] INSERT INTO "notification" ("id","tittle","content","status","note","notification_mark_id","created_at","updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) []interface {}{"88409259305336832", "test", "test", 0, "", interface {}(nil), "2019-01-06 22:12:22", "2019-01-06 22:12:22"} {Id:88409259305336832 Tittle:test Content:test Status:0 Note: Mark:<nil> CreatedAt:0001-01-01 00:00:00 +0000 UTC UpdatedAt:0001-01-01 00:00:00 +0000 UTC DeletedAt:<nil>} [xorm] [info] 2019/01/06 22:12:22.988461 [SQL] COMMIT ``` 可以看到传入的 `&n`没有被赋值时间 `CreatedAt:0001-01-01 00:00:00 +0000 UTC`

Could you show your struct definition?

Could you show your struct definition?
axetroy commented 2019-01-08 15:35:20 +00:00 (Migrated from github.com)

@lunny

here is the struct definition

type Notification struct {
	Id        string             `xorm:"pk notnull unique index varchar(32)" json:"id"` // 通知ID
	Tittle    string             `xorm:"notnull index varchar(32)" json:"tittle"`       // 公告标题
	Content   string             `xorm:"notnull text" json:"content"`                   // 公告内容
	Status    NotificationStatus `xorm:"notnull" json:"status"`                         // 公告状态
	Note      string             `xorm:"null varchar(255)" json:"note"`                 // 这条通知的备注
	Mark      *NotificationMark  `xorm:"'notification_mark_id'" json:"mark"`
	CreatedAt time.Time          `xorm:"created" json:"created_at"`
	UpdatedAt time.Time          `xorm:"updated" json:"updated_at"`
	DeletedAt *time.Time         `xorm:"deleted" json:"deleted_at"`
}
@lunny here is the struct definition ```go type Notification struct { Id string `xorm:"pk notnull unique index varchar(32)" json:"id"` // 通知ID Tittle string `xorm:"notnull index varchar(32)" json:"tittle"` // 公告标题 Content string `xorm:"notnull text" json:"content"` // 公告内容 Status NotificationStatus `xorm:"notnull" json:"status"` // 公告状态 Note string `xorm:"null varchar(255)" json:"note"` // 这条通知的备注 Mark *NotificationMark `xorm:"'notification_mark_id'" json:"mark"` CreatedAt time.Time `xorm:"created" json:"created_at"` UpdatedAt time.Time `xorm:"updated" json:"updated_at"` DeletedAt *time.Time `xorm:"deleted" json:"deleted_at"` } ```

I think this has been resolved.

I think this has been resolved.
lunny closed this issue 2021-08-04 08:17:44 +00:00
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#1184
No description provided.