mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-11 02:54:23 +02:00
Create table now works for pure m2m where both rows are foreign keys
This commit is contained in:
parent
56e29158fe
commit
acea54877c
2 changed files with 46 additions and 27 deletions
|
|
@ -25,26 +25,37 @@ class Database:
|
||||||
).fetchall()
|
).fetchall()
|
||||||
]
|
]
|
||||||
|
|
||||||
def create_table(self, name, columns, pk=None):
|
def create_table(self, name, columns, pk=None, foreign_keys=None):
|
||||||
|
foreign_keys = foreign_keys or []
|
||||||
|
foreign_keys_by_name = {fk[0]: fk for fk in foreign_keys}
|
||||||
|
extra = ""
|
||||||
|
columns = ",\n".join(
|
||||||
|
" {col_name} {col_type} {primary_key} {references}".format(
|
||||||
|
col_name=col_name,
|
||||||
|
col_type={
|
||||||
|
float: "FLOAT",
|
||||||
|
int: "INTEGER",
|
||||||
|
bool: "INTEGER",
|
||||||
|
str: "TEXT",
|
||||||
|
None.__class__: "TEXT",
|
||||||
|
}[col_type],
|
||||||
|
primary_key=" PRIMARY KEY" if (pk == col_name) else "",
|
||||||
|
references=(
|
||||||
|
" REFERENCES [{other_table}({other_column})]".format(
|
||||||
|
other_table=foreign_keys_by_name[col_name][2],
|
||||||
|
other_column=foreign_keys_by_name[col_name][3],
|
||||||
|
)
|
||||||
|
if col_name in foreign_keys_by_name
|
||||||
|
else ""
|
||||||
|
),
|
||||||
|
)
|
||||||
|
for col_name, col_type in columns.items()
|
||||||
|
)
|
||||||
sql = """CREATE TABLE {table} (
|
sql = """CREATE TABLE {table} (
|
||||||
{columns}
|
{columns}
|
||||||
);
|
){extra};
|
||||||
""".format(
|
""".format(
|
||||||
table=name,
|
table=name, columns=columns, extra=extra
|
||||||
columns=",\n".join(
|
|
||||||
" {col_name} {col_type} {primary_key}".format(
|
|
||||||
col_name=col_name,
|
|
||||||
col_type={
|
|
||||||
float: "FLOAT",
|
|
||||||
int: "INTEGER",
|
|
||||||
bool: "INTEGER",
|
|
||||||
str: "TEXT",
|
|
||||||
None.__class__: "TEXT",
|
|
||||||
}[col_type],
|
|
||||||
primary_key=" PRIMARY KEY" if (pk == col_name) else "",
|
|
||||||
)
|
|
||||||
for col_name, col_type in columns.items()
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
self.conn.execute(sql)
|
self.conn.execute(sql)
|
||||||
return self[name]
|
return self[name]
|
||||||
|
|
@ -66,16 +77,8 @@ class Table:
|
||||||
return [Column(*row) for row in rows]
|
return [Column(*row) for row in rows]
|
||||||
|
|
||||||
def create(self, columns, pk=None, foreign_keys=None):
|
def create(self, columns, pk=None, foreign_keys=None):
|
||||||
# Ignore columns in foreign_keys list
|
columns = {name: value for (name, value) in columns.items()}
|
||||||
columns = {
|
self.db.create_table(self.name, columns, pk=pk, foreign_keys=foreign_keys)
|
||||||
name: value
|
|
||||||
for name, value in columns.items()
|
|
||||||
if name not in {fk[0] for fk in (foreign_keys or [])}
|
|
||||||
}
|
|
||||||
self.db.create_table(self.name, columns, pk=pk)
|
|
||||||
if foreign_keys:
|
|
||||||
for args in foreign_keys:
|
|
||||||
self.add_foreign_key(*args)
|
|
||||||
self.exists = True
|
self.exists = True
|
||||||
|
|
||||||
def drop(self):
|
def drop(self):
|
||||||
|
|
|
||||||
|
|
@ -38,3 +38,19 @@ def test_create_table_from_example(fresh_db, example, expected_columns):
|
||||||
assert expected_columns == [
|
assert expected_columns == [
|
||||||
{"name": col.name, "type": col.type} for col in fresh_db["people"].columns
|
{"name": col.name, "type": col.type} for col in fresh_db["people"].columns
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_table_works_for_m2m_with_only_foreign_keys(fresh_db):
|
||||||
|
fresh_db["one"].insert({"id": 1}, pk="id")
|
||||||
|
fresh_db["two"].insert({"id": 1}, pk="id")
|
||||||
|
fresh_db["m2m"].insert(
|
||||||
|
{"one_id": 1, "two_id": 1},
|
||||||
|
foreign_keys=(
|
||||||
|
("one_id", "INTEGER", "one", "id"),
|
||||||
|
("two_id", "INTEGER", "two", "id"),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
assert [
|
||||||
|
{"name": "one_id", "type": "INTEGER"},
|
||||||
|
{"name": "two_id", "type": "INTEGER"},
|
||||||
|
] == [{"name": col.name, "type": col.type} for col in fresh_db["m2m"].columns]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue