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

Question on how to create a proper Many to many relation between enitites #158

Open
wangyihanlarry opened this issue Dec 20, 2022 · 1 comment
Labels
more added when Josiah is waiting for more from op question

Comments

@wangyihanlarry
Copy link

Hi,

I need to store two entites, one is file system and another is client. they are mapped many to many in real world, means, multiple FSes could be mapped to one client, while mulit clients could be mapped to one FS at mean time, below is an exmpale,

I created two FS, FS_1, and FS_2
I then exposed FS1 to Client_1 and Client_2
I then exposed FS2 to Client_1 and Client_2

Then I am using the following models to persis them into the redis

class Client(rom.Model):
    ip = rom.String(required=True, unique=True, index=True, prefix=True, suffix=True, keygen=rom.IDENTITY)
    entries = rom.ManyToOne('Entry', on_delete='no action')

class Entry(rom.Model):
    client = rom.OneToMany('Client')
    fs = rom.OneToMany('FileSystem')

    
class FileSystem(rom.Model):
    fs_name = rom.String(required=True, unique=True, index=True, prefix=True, suffix=True, keygen=rom.IDENTITY)
    fs_type = rom.String(required=True )
    fs_creation_time = rom.DateTime(required=True)
    fs_mount_point = rom.String(required=True)
    fs_is_mounted = rom.Boolean(required=True)
    fs_is_dirty = rom.Boolean(required=True)
    fs_is_deleted = rom.Boolean(required=True)
    entries = rom.ManyToOne('Entry', on_delete='no action')

Them,I am going to use them like in below

entry1 = Entry()

    client1 = Client(ip='1.2.3.4')
    client1.entries=entry1
    client1.save()

    client2 = Client(ip='5.2.3.4')
    client2.entries=entry1
    client2.save()

    fs_1 = FileSystem(fs_name='fs_1',
                        fs_type ='NFS',
                        fs_creation_time=datetime.now(), 
                        fs_mount_point='/tmp{}'.format(0),
                        fs_is_mounted=random.choice([True,False]),
                        fs_is_dirty=random.choice([True,False]), 
                        fs_is_deleted=random.choice([True,False]))
    fs_1.entries=entry1
    fs_1.save()
    

    fs_2 = FileSystem(fs_name='fs_2',
                        fs_type ='NFS',
                        fs_creation_time=datetime.now(), 
                        fs_mount_point='/tmp{}'.format(1),
                        fs_is_mounted=random.choice([True,False]),
                        fs_is_dirty=random.choice([True,False]), 
                        fs_is_deleted=random.choice([True,False]))
    fs_2.entries=entry1
    fs_2.save()

However, I found it is quite inconnvient to add data on it, saying, I am now going to exposed FS3 to Client_1 only, what should I supposed to do?

@josiahcarlson
Copy link
Owner

josiahcarlson commented Dec 27, 2022 via email

@josiahcarlson josiahcarlson added question more added when Josiah is waiting for more from op labels Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
more added when Josiah is waiting for more from op question
Projects
None yet
Development

No branches or pull requests

2 participants