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

Added Foreign & Composite Primary Key support to mysqloo driver #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

nodgear
Copy link
Contributor

@nodgear nodgear commented Sep 2, 2022

This change provides the following features:

  • Composite primary key
  • Foreign key relations

Both without breaking changes.

Example model:

-- Creates a table with composite primary key with one of them being foreign
sqlier.Model({
 Table = "experience",
 Columns = {
   charid = {
     Type = sqlier.Type.Integer,
     Relation = {
       Table = "character",
       Column = "id"
     }
   },
   category = {
     Type = sqlier.Type.String,
     MaxLength = 120
   },
   amount = {
     Type = sqlier.Type.Integer,
     Default = 0
   }
 },
 Identity = { "charid", "category" }
})

Limitation

The limitation is the same as many other ORM's/Abstractions:
Tables must be created in the correct order otherwise sgdb will say reference table/column doesn't exist.
This limitation should be easy to overcome and natural to any developer.

Copy link
Contributor Author

@nodgear nodgear left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot debug in there

@ceifa
Copy link
Owner

ceifa commented Sep 2, 2022

Hey, thank you for your PR.

Great addition on composite identities!
About the foreign key, I'm not sure if this is something sqlier should solve. One of the main ideas of sqlier is, write model code once and run it in any database, and things like that break it. Tbh on my opinion foreign key constraints doesn't bring real benefits and can be solved on the application layer.

@ceifa
Copy link
Owner

ceifa commented Sep 2, 2022

Anyway, just for reference, this is how sequelize deal with relationships: https://sequelize.org/docs/v6/core-concepts/assocs/

@ceifa
Copy link
Owner

ceifa commented Sep 2, 2022

A good relationship code suggestion:

local User = sqlier.Model({
    Table = "user",
    Columns = {
        Rank = {
            Type = sqlier.Type.String,
            MaxLength = 15
        },
        SteamId64 = {
            Type = sqlier.Type.SteamId64
        }
    },
    Identity = "SteamId64"
})

local Character = sqlier.Model({
    Table = "character",
    Columns = {
        Id = {
            Type = sqlier.Type.Integer,
            AutoIncrement = true
        },
        Name = {
            Type = sqlier.Type.String
        },
        UserId = User,
        Identity = "Id"
    }
})

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

Successfully merging this pull request may close these issues.

2 participants