fukamachi / mito

An ORM for Common Lisp with migrations, relationships and PostgreSQL support
292 stars 31 forks source link

DB Error: NOT NULL constraint failed: content.user_id (Code: CONSTRAINT) #58

Closed rnfn closed 5 years ago

rnfn commented 5 years ago

CL-USER> (ql:quickload :mito) CL-USER> (mito:connect-toplevel sqlite3 :database-name "mito-test.db") CL-USER> (mito:deftable user () ((user :col-type :text :initarg :user :accessor username))) CL-USER> (mito:ensure-table-exists 'user) ;; CREATE TABLE "user" ( ;; "id" INTEGER PRIMARY KEY AUTOINCREMENT, ;; "user" TEXT NOT NULL, ;; "created_at" TIMESTAMP, ;; "updated_at" TIMESTAMP ;; ) () [0 rows] | MITO.DB:EXECUTE-SQL (#<SXQL-STATEMENT: CREATE TABLE user ( id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT NOT NULL, created_at TIMESTAMP, updated_at TIMESTAMP )>) CL-USER> (mito:deftable content () ((body :col-type :text :initarg :body :accessor :content-body) (user :col-type user)))

CL-USER> (mito:ensure-table-exists 'content) ;; CREATE TABLE "content" ( ;; "id" INTEGER PRIMARY KEY AUTOINCREMENT, ;; "body" TEXT NOT NULL, ;; "user_id" INTEGER NOT NULL, ;; "created_at" TIMESTAMP, ;; "updated_at" TIMESTAMP ;; ) () [0 rows] | MITO.DB:EXECUTE-SQL (#<SXQL-STATEMENT: CREATE TABLE content ( id INTEGER PRIMARY KEY AUTOINCREMENT, body TEXT NOT NULL, user_id INTEGER NOT NULL, created_at TIMESTAMP, updated_at TIMESTAMP )>) CL-USER> (defvar test-user (mito:create-dao 'user :user "rnfn")) CL-USER> (mito:create-dao 'content :body "test content" :user test-user)

I follow the relationship tutorial, but i always get "DB Error: NOT NULL constraint failed: content.user_id (Code: CONSTRAINT)" when inserting table. Database is sqlite3.

fukamachi commented 5 years ago

Perhaps Mito doesn't add :initarg to relational slots. Workaround is adding :initarg :user to the user slot.

fukamachi commented 5 years ago

Note: mito:create-dao doesn't raise an error for unexpected initargs since it specifies :allow-other-keys t. (It's added at 55fe8d89)