Coder Social home page Coder Social logo

Comments (4)

simonw avatar simonw commented on May 13, 2024

Maybe foreign_keys could even optionally just be a list of columns - it could then attempt to detect the related tables based on some rules-of-thumb and raise an error if there's no obvious candidate.

Rules:

  • If the column name ends in _id, remove that suffix and look for a matching table.
  • Try for a table which is the column name without the _id suffix with an s appended to it
  • Try for a table that's the exact match for the column name

If none of these rules match, raise an error.

So the above example could be further simplified to:

db["usages"].insert_all(
    usages_to_insert,
    foreign_keys=["line_id", "definition_id"]
)

from sqlite-utils.

simonw avatar simonw commented on May 13, 2024

If I'm going to do this then I should make the other_table and other_column arguments optional here too:

@click.argument("table")
@click.argument("column")
@click.argument("other_table")
@click.argument("other_column")
def add_foreign_key(path, table, column, other_table, other_column):
"""
Add a new foreign key constraint to an existing table. Example usage:
$ sqlite-utils add-foreign-key my.db books author_id authors id
WARNING: Could corrupt your database! Back up your database file first.
"""
db = sqlite_utils.Database(path)
try:
db[table].add_foreign_key(column, other_table, other_column)

from sqlite-utils.

simonw avatar simonw commented on May 13, 2024

Still need to add this mechanism to .create_table() - this code here is all that needs to be modified - it needs to learn to deal with the alternative syntax for foreign keys and guess the missing data if necessary:

def create_table(
self, name, columns, pk=None, foreign_keys=None, column_order=None, hash_id=None
):
foreign_keys = foreign_keys or []
foreign_keys_by_name = {fk[0]: fk for fk in foreign_keys}

from sqlite-utils.

simonw avatar simonw commented on May 13, 2024

I'm going to reuse the ForeignKey named tuple here:

ForeignKey = namedtuple(
"ForeignKey", ("table", "column", "other_table", "other_column")
)

from sqlite-utils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.