Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid code for implicitly Notnull Primary Keys with SQlite #22730

Closed
shful opened this issue Nov 15, 2017 · 3 comments
Closed

Invalid code for implicitly Notnull Primary Keys with SQlite #22730

shful opened this issue Nov 15, 2017 · 3 comments

Comments

@shful
Copy link

shful commented Nov 15, 2017

What version of Go are you using (go version)?

go 1.9

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

darwin/amd64

What did you do?

Generate code with XO for a productive Sqlite3 database.

An integer Primary Key column which is implicitly, but not explicitly declared as "not null", causes invalid code.

CREATE TABLE revinfo (
  rev INTEGER primary key
);

XO generates this revinfo.xo.go which does not compile:


// Revinfo represents a row from 'revinfo'.
type Revinfo struct {
	Rev sql.NullInt64 `json:"rev"` // rev

	// xo fields
	_exists, _deleted bool
}

// ...

// Insert inserts the Revinfo to the database.
func (r *Revinfo) Insert(db XODB) error {
	var err error

	// if already exist, bail
	if r._exists {
		return errors.New("insert failed: already exists")
	}

	// sql insert query, primary key provided by autoincrement
	const sqlstr = `INSERT INTO revinfo (` +
		`` +
		`) VALUES (` +
		`` +
		`)`

	// run query
	XOLog(sqlstr)
	res, err := db.Exec(sqlstr)
	if err != nil {
		return err
	}

	// retrieve id
	id, err := res.LastInsertId()
	if err != nil {
		return err
	}

	// set primary key and existence
	r.Rev = sql.NullInt64(id) // <<<<< Compile Error because invalid conversion
	r._exists = true

	return nil
}

Analogously, all Foreign Key references to the "rev" column generate the same invalid conversion in their files.

Note 1: Such "revinfo" tables are generated by Hibernate Envers.
Note 2: There is a historical bug in SQlite so that Primary Keys indeed can be Nullable, which does not conform to the SQL standard.
I'd find it acceptable that XO creates invalid code where SQlite allows invalid DDL :-)
But this does not apply here. The "rev" column is of type "integer" and in this special case, SQlite conforms to the SQL standard and implicitly defines the Primary Key as "Not Null".

Suggestion: XO could implicitly assume Primary Keys to be "Not Null". This should fit the SQL standard.

personal limitation#1 I'm aware that implicit behaviour needs to fit in any case, and I expect to miss some implications.
personal limitation#2 I'm unsure whom to blame. Eventually, the SQlite driver should already report the Primary Key as "Not Null".

@gbbr
Copy link
Member

gbbr commented Nov 15, 2017

This doesn't seem related to any feature of the Go programming language. I think you probably want to close this and open an issue in https://github.com/xo/xo (I'm assuming this is the package that you're referring to here).

@bradfitz
Copy link
Contributor

Yeah, like @gbbr says, it seems that you have the wrong issue tracker. I've never heard of xo.

@shful
Copy link
Author

shful commented Nov 15, 2017

Absolutely. I see it was posted here by accident. I'm very sorry!!
Could you purge the whole posting, please ?
(gbbr was right about the xo package)

@golang golang locked and limited conversation to collaborators Nov 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants