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

Fix/760 #762

Merged
merged 4 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/masoniteorm/commands/MigrateRefreshCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def handle(self):
connection=self.option("connection"),
migration_directory=self.option("directory"),
config_path=self.option("config"),
schema=self.option("schema")
schema=self.option("schema"),
)

migration.refresh(self.option("migration"))
Expand Down
3 changes: 2 additions & 1 deletion src/masoniteorm/commands/stubs/table_migration.stub
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ class __MIGRATION_NAME__(Migration):
"""
Revert the migrations.
"""
pass
with self.schema.table("__TABLE_NAME__") as table:
pass
4 changes: 3 additions & 1 deletion src/masoniteorm/connections/ConnectionResolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def get_schema_builder(self, connection="default", schema=None):
from ..schema import Schema

return Schema(
connection=connection, connection_details=self.get_connection_details(), schema=schema
connection=connection,
connection_details=self.get_connection_details(),
schema=schema,
)

def get_query_builder(self, connection="default"):
Expand Down
21 changes: 14 additions & 7 deletions src/masoniteorm/migrations/Migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ def __init__(
DATABASES = DB.get_connection_details()

self.schema = Schema(
connection=connection, connection_details=DATABASES, dry=dry, schema=self.schema_name
connection=connection,
connection_details=DATABASES,
dry=dry,
schema=self.schema_name,
)

self.migration_model = MigrationModel.on(self.connection)
Expand Down Expand Up @@ -137,7 +140,9 @@ def migrate(self, migration="all", output=False):
f"<comment>Migrating:</comment> <question>{migration}</question>"
)

migration_class = migration_class(connection=self.connection, schema=self.schema_name)
migration_class = migration_class(
connection=self.connection, schema=self.schema_name
)

if output:
migration_class.schema.dry()
Expand Down Expand Up @@ -187,7 +192,9 @@ def rollback(self, migration="all", output=False):
self.command_class.line(f"<error>Not Found: {migration}</error>")
continue

migration_class = migration_class(connection=self.connection, schema=self.schema_name)
migration_class = migration_class(
connection=self.connection, schema=self.schema_name
)

if output:
migration_class.schema.dry()
Expand Down Expand Up @@ -237,9 +244,7 @@ def reset(self, migration="all"):

if not len(migrations):
if self.command_class:
self.command_class.line(
"<info>Nothing to reset</info>"
)
self.command_class.line("<info>Nothing to reset</info>")
else:
print("Nothing to reset")

Expand All @@ -250,7 +255,9 @@ def reset(self, migration="all"):
)

try:
self.locate(migration)(connection=self.connection, schema=self.schema_name).down()
self.locate(migration)(
connection=self.connection, schema=self.schema_name
).down()
except TypeError:
self.command_class.line(f"<error>Not Found: {migration}</error>")
continue
Expand Down
2 changes: 1 addition & 1 deletion src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ def detach_many(self, relation, relating_records):
else:
related_record.save()

related.detach_related(self, related_record)
related.detach(self, related_record)

def related(self, relation):
related = getattr(self.__class__, relation)
Expand Down
2 changes: 1 addition & 1 deletion src/masoniteorm/query/QueryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ def update(self, updates: dict, dry=False, force=False):
if model and not model.__force_update__ and not force:
changes = {}
for attribute, value in updates.items():
if model.__original_attributes__.get(attribute, None) != value:
if model.__original_attributes__.get(attribute, None) != value or value is None:
changes.update({attribute: value})
updates = changes

Expand Down
8 changes: 3 additions & 5 deletions src/masoniteorm/relationships/BaseRelationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ def get_with_count_query(self, builder, callback):
return return_query

def attach(self, current_model, related_record):
return current_model.update(
{self.local_key: getattr(related_record, self.foreign_key)}
return related_record.update(
{self.foreign_key: getattr(current_model, self.local_key)}
)

def get_related(self, query, relation, eagers=None, callback=None):
Expand Down Expand Up @@ -183,9 +183,7 @@ def relate(self, related_record):
)

def detach(self, current_model, related_record):
return current_model.where(
{self.local_key: getattr(related_record, self.foreign_key)}
).delete()
return related_record.update({self.foreign_key: None})

def attach_related(self, current_model, related_record):
return related_record.update(
Expand Down
2 changes: 1 addition & 1 deletion src/masoniteorm/relationships/BelongsToMany.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def detach(self, current_model, related_record):
.table(self._table)
.without_global_scopes()
.where(data)
.delete()
.update({self.foreign_key: None, self.local_key: None})
)

def attach_related(self, current_model, related_record):
Expand Down
8 changes: 5 additions & 3 deletions src/masoniteorm/schema/Schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,11 @@ def new_connection(self):
if self._dry:
return

self._connection = self.connection_class(
**self.get_connection_information()
).set_schema(self.schema).make_connection()
self._connection = (
self.connection_class(**self.get_connection_information())
.set_schema(self.schema)
.make_connection()
)

return self._connection

Expand Down
4 changes: 3 additions & 1 deletion tests/mysql/relationships/test_relationships.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ def test_relationship_where_has(self):
def test_relationship_where_has_nested(self):
sql = (
User.where("name", "Joe")
.where_has("profile.identification", lambda q: q.where("identification_id", 1))
.where_has(
"profile.identification", lambda q: q.where("identification_id", 1)
)
.to_sql()
)

Expand Down