moigagoo / norm

A Nim ORM for SQLite and Postgres
https://norm.nim.town
MIT License
378 stars 34 forks source link

Can be ID columnt created first? #191

Closed krab closed 1 year ago

krab commented 1 year ago

Currently, it's always last https://i.imgur.com/2NmEnSO.png

And nim code looks like:

type
  User* = ref object of Model
    user_id*: int
    firstname*: string
    lastname*: string
    username*: string

  Feed* = ref object of Model
    channel_id*: string
    playlist_id*: string
    video_id*: string
    name*: string
    url*: string

  UserFeed* = ref object of Model
    user*: User
    feed*: Feed

func newUser*(user_id = 0, firstname = "", lastname = "", username = ""): User =
  return User(user_id: user_id, firstname: firstname, lastname: lastname, username: username)

func newFeed*(channel_id = "", playlist_id = "", video_id = "", name = "", url = ""): Feed =
  return Feed(channel_id: channel_id, playlist_id: playlist_id, video_id: video_id, name: name, url: url)

func newUserFeed*(user = newUser(), feed = newFeed()): UserFeed =
  return UserFeed(user: user, feed: feed)

let dbConn* = open("/home/krab/NimProjects/yatb/test.db", "", "", "")

when isMainModule:
  dbConn.createTables(newUser())
  dbConn.createTables(newFeed())
  dbConn.createTables(newUserFeed())
moigagoo commented 1 year ago

Column order doesn't mean anything really as far as I know. It's random and you should never rely on it.

What is the problem you're solving?

krab commented 1 year ago

What is the problem you're solving?

No problems at all. Just every single ORM (based on structs->DB) for Go makes ID columns first just it. This is the first time I see random.

moigagoo commented 1 year ago

What is the problem you're solving?

No problems at all. Just every single ORM (based on structs->DB) for Go makes ID columns first just it. This is the first time I see random.

What I meant by saying that the column order is random is that the list of columns is unordered in general and can't be relied upon. But I guess in some DBs and in some cases the insertion order would match the order the columns appear in selects.

It shouldn't really be a problem to insert id first. I'm just not seeing the benefit of potentially breaking someone's code with this change.