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

Field name problem #5

Open
soegaard opened this issue Sep 26, 2019 · 6 comments
Open

Field name problem #5

soegaard opened this issue Sep 26, 2019 · 6 comments
Labels
enhancement New feature or request

Comments

@soegaard
Copy link

soegaard commented Sep 26, 2019

The following program provokes an error:
; prepare: no such column: GITHUB
I suspect that (somewhere) there an "deta field name to sql fieldname" conversion missing.

#lang racket
(require deta db threading)

(define db
  (sqlite3-connect #:database "bug.db"
                   #:mode     'create))

(define-schema github-user
  ([id         id/f        #:primary-key #:auto-increment]
   [user-id    integer/f]            ; racket-stories user-id   
   [github-id  integer/f   #:unique] ; github user id
   [login      string/f    #:unique] ; github login (username)
   [real-name  string/f]             
   [email      string/f]              
   [avatar-url string/f]
   [blog-url   string/f]))


(create-table! db 'github-user)

(lookup db (~> (from github-user #:as gu)
               (where (= github-id 123))))

the full error:

; prepare: no such column: GITHUB
;   error code: 1
; Context:
;  /Applications/Racket v7.4/collects/db/private/sqlite3/connection.rkt:466:0 handle-status*
;  /Applications/Racket v7.4/collects/db/private/sqlite3/connection.rkt:422:8
;  /Applications/Racket v7.4/collects/db/private/sqlite3/connection.rkt:225:4 prepare1* method in connection%
;  /Users/soegaard/Library/Racket/7.4/pkgs/deta-lib/private/query.rkt:23:2
;  /Applications/Racket v7.4/collects/db/private/generic/functions.rkt:90:0 compose-statement
;  /Applications/Racket v7.4/share/pkgs/db-lib/db/private/generic/functions2.rkt:64:0
;  /Users/soegaard/Library/Racket/7.4/pkgs/deta-lib/query.rkt:254:0 unpack219
;  /Users/soegaard/Library/Racket/7.4/pkgs/deta-lib/query.rkt:267:0 lookup
;  "/Users/soegaard/tmp/bug.rkt":1:1 [running body]
; [Due to errors, REPL is just module language, requires, and stub definitions]

@Bogdanp
Copy link
Owner

Bogdanp commented Sep 26, 2019

I think the issue is that you haven't prefixed github-id with gu in your query. This works:


(lookup db (~> (from github-user #:as gu)
               (where (= gu.github-id 123))))

@soegaard
Copy link
Author

soegaard commented Sep 26, 2019 via email

@Bogdanp
Copy link
Owner

Bogdanp commented Sep 26, 2019

Yes, I think we can detect cases like this, where the identifier contains a dash. However, I don't think this can be detected in general.

@Bogdanp Bogdanp added the enhancement New feature or request label Sep 26, 2019
@soegaard
Copy link
Author

Actually for the user table I can do:

(define (get-user/id id)
  (lookup db (~> (from user #:as u) (where (= id ,id)))))

That is, it works even when I don't write u.id.
By analogy (where (= github-id 123)))) ought to work too.

@Bogdanp
Copy link
Owner

Bogdanp commented Sep 26, 2019

By analogy (where (= github-id 123)))) ought to work too.

I would say it's the reverse.

(= id ,id)

IMO, this and any use of an unqualified column name should error out, but it doesn't because the parser for query expressions is not smart enough to distinguish between cases where an identifier refers to the name of a column and cases where an identifier refers to some other identifiers within the database. I could make the parser error out whenever a bare identifier is found anywhere except in function name position, but then stuff like

(cast 1 text)

would also fail to parse (on text) so we'd have to come up with some other way to refer to things that are not columns or functions or special-case casting. The latter would work, but I'm sure that are other cases where you might want to refer to functions or types by name that I can't think of right now.

The easiest solution would be to have things continue working as they do now and just replace -s with _s within identifiers. I think that's what I'll do for now, but I'd recommend always qualifying column names in case I find a solution to the problem as a whole that I like somewhere down the line and break this.

@Bogdanp
Copy link
Owner

Bogdanp commented Sep 26, 2019

The easiest solution would be to have things continue working as they do now and just replace -s with _s within identifiers.

I spoke too soon and didn't take... well... -, along with a couple other operators like array-slice, into consideration.

I think we might be stuck with this behavior for the time being. I'll have to make a list of all the situations in which bare identifiers can be used and then see if I can't come up with a better solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants